# 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:

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

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

1. Set odd_sum  = 0 and even_sum = 0.
2. Start scanning the array element. For each array element, check if it even or odd.
3. 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;
}

```
```OUTPUT:

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: 3
Time Complexity:
O(n),  where n is the size of array```

Related Posts: