# Find absolute difference between odd and even elements of the array

“** Find absolute difference between odd and even elements of the array**” is an elementary problem of array data structure for beginners. Here, we are given an array of size ‘n’ and our task is to find the absolute difference between odd and even elements of an array.

**Example:**

Array = {1, 2, 3, 4, 5}INPUT:3OUTPUT:Sum of odd elements = (1+3+5) = 9 Sum of even elements = (2+4) = 6 Absolute difference = abs(9-6) = 3Explanation:

The steps required to find the absolute difference between odd and even elements of the array are as follows:

- Set odd_sum = 0 and even_sum = 0.
- Start scanning the array element. For each array element, check if it even or odd.
- If the array element is even number then, add element with even_sum by:

even_sum = even_sum + arr[i]

4. If the array element is odd number then, add element with odd_count by

odd_sum = odd_sum + arr[i].

5. abs_diff = (even_sum > odd_sum) ? (even_sum – odd_sum) : (even_sum – odd_sum).

6. Print thr abs_diff.

**C++ Program to find absolute difference between odd and even elements of the array is as follows:**

```
/* Program to find absolute difference between sum of
even and sum of odd elements in an array */
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
/* Scanning size of the array */
cout<<"Enter the number of array elements: ";
cin>>n;
/* Creating an Array */
int arr[n];
/* Scanning the array elements */
cout<<"\nEnter the array elements: ";
for(int i = 0; i < n; i++)
cin>>arr[i];
int odd_sum = 0;
int even_sum = 0;
/* Getting even sum and odd sum */
for(int i = 0; i < n; i++)
{
if(arr[i] % 2 == 0)
even_sum = even_sum + arr[i];
else if(arr[i] % 2 != 0)
odd_sum = odd_sum + arr[i];
}
/* Finding absolute difference */
int abs_diff = (even_sum > odd_sum) ? (even_sum - odd_sum) : (odd_sum - even_sum);
/* Printing the result */
cout<<"\nThe absolute difference between sum of even number and sum of odd numbers is: "<<abs_diff;
}
```

Enter the number of array elements: 5 Enter the array elements: 1 2 3 4 5 The absolute difference between sum of even number and sum of odd numbers is: 3OUTPUT:O(n), where n is the size of arrayTime Complexity:

**Related Posts:**

- Program to find first repeating element of an array.
- Program to merge two sorted arrays.
- Program to find missing number in an array.
- Program to sort if array is sorted.
- Program to print Alternate Elements of an Array.
- Program to swap kth element from beginning to kth element from end in an Array.
- Program to print all possible subarrays of the given array.
- Program to print kth smallest and kth largest element of an Array.
- Program to find equilibrium index of an Array.
- Program to find majority element of an Array.
- Program to find mean of the Array.
- Program to sort an Array of 0s and 1s.
- Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
- Program to count total number of words in a string.
- Program to find smallest and largest word of the String.
- Program to find Most Frequent Character of the String.
- Program to Remove all the blank spaces from the String.
- Program to check if String is isogram or not.
- Program to Reverse Each word of the String.
- Program to Print All the Substring of the Given String.