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:

  1. Program to find smallest and largest element of the array.
  2. Program to count occurrences of a particular element in an array.
  3. Program to search an element in an Array (Linear Search).
  4. Program to Rotate Array Elements by ‘D’ positions.
  5. Program to find most frequent element in an array.
  6. Program to find pair in an array with given sum.
  7. Program to find first repeating element of an array.
  8. Program to merge two sorted arrays.
  9. Program to find missing number in an array.
  10. Program to sort if array is sorted.
  11. Program to print Alternate Elements of an Array.
  12. Program to swap kth element from beginning to kth element from end in an Array.
  13. Program to print all possible subarrays of the given array.
  14. Program to print kth smallest and kth largest element of an Array.
  15. Program to find equilibrium index of an Array.
  16. Program to find majority element of an Array.
  17. Program to find mean of the Array.
  18. Program to sort an Array of 0s and 1s.
  19. Program to Reverse an Array.
  20. Program to count number of odd numbers and even numbers in an array.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *