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 first repeating element of an array.
  2. Program to merge two sorted arrays.
  3. Program to find missing number in an array.
  4. Program to sort if array is sorted.
  5. Program to print Alternate Elements of an Array.
  6. Program to swap kth element from beginning to kth element from end in an Array.
  7. Program to print all possible subarrays of the given array.
  8. Program to print kth smallest and kth largest element of an Array.
  9. Program to find equilibrium index of an Array.
  10. Program to find majority element of an Array.
  11. Program to find mean of the Array.
  12. Program to sort an Array of 0s and 1s.
  13. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  14. Program to count total number of words in a string.
  15. Program to find smallest and largest word of the String.
  16. Program to find Most Frequent Character of the String.
  17. Program to Remove all the blank spaces from the String.
  18. Program to check if String is isogram or not.
  19. Program to Reverse Each word of the String.
  20. Program to Print All the Substring of the Given String.

You may also like...

Leave a Reply

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