Difference between sum of odd and even frequent elements in an Array
Given an array arr[] of integers, the task is to find the absolute difference between the sum of all odd frequent array elements and the sum of all even frequent array elements.
Examples:
Input: arr[] = {1, 5, 5, 2, 4, 3, 3}
Output: 9
Explanation:
The even frequent elements are 5 and 3 (both occurring twice).
Therefore, sum of all even frequent elements = 5 + 5 + 3 + 3 = 16.
The odd frequent elements are 1, 2 and 4 (each occurring once).
Therefore, sum of all odd frequent elements = 1 + 2 + 4 = 7.
Difference between their sum = 16 – 7 = 9.Input: arr[] = {1, 1, 2, 2, 3, 3}
Output: 12
Explanation:
The even frequent array elements are 1, 2 and 3 (occurring twice).
Therefore, sum of all even frequent elements = 12.
Since there is no odd frequent element present in the array, difference = 12 – 0 = 12
Approach: Follow the steps below to solve the problem:
- Initialize an unordered_map to store the frequency of array elements.
- Traverse the array and update the frequency of array elements in the Map.
- Then, traverse the map and add the elements having even frequency to a variable, say sum_even, and the ones with odd frequencies to another variable, say sum_odd.
- Finally, print the difference between sum_odd and sum_even.
Below is the implementation of the above approach:
C++
// C++ program to find absolute difference // between the sum of all odd frequent and // even frequent elements in an array #include <bits/stdc++.h> using namespace std; // Function to find the sum of all even // and odd frequent elements in an array int findSum( int arr[], int N) { // Stores the frequency of array elements unordered_map< int , int > mp; // Traverse the array for ( int i = 0; i < N; i++) { // Update frequency of // current element mp[arr[i]]++; } // Stores sum of odd and even // frequent elements int sum_odd = 0, sum_even = 0; // Traverse the map for ( auto itr = mp.begin(); itr != mp.end(); itr++) { // If frequency is odd if (itr->second % 2 != 0) // Add sum of all occurrences of // current element to sum_odd sum_odd += (itr->first) * (itr->second); // If frequency is even if (itr->second % 2 == 0) // Add sum of all occurrences of // current element to sum_even sum_even += (itr->first) * (itr->second); } // Calculate difference // between their sum int diff = sum_even - sum_odd; // Return diff return diff; } // Driver Code int main() { int arr[] = { 1, 5, 5, 2, 4, 3, 3 }; int N = sizeof (arr) / sizeof (arr[0]); cout << findSum(arr, N); return 0; } |
Java
// Java program to find absolute difference // between the sum of all odd frequenct and // even frequent elements in an array import java.util.*; import java.io.*; import java.math.*; class GFG{ // Function to find the sum of all even // and odd frequent elements in an array static int findSum( int arr[], int N) { // Stores the frequency of array elements Map<Integer, Integer> map = new HashMap<Integer, Integer>(); // Traverse the array for ( int i = 0 ; i < N; i++) { // Update frequency of // current element if (!map.containsKey(arr[i])) map.put(arr[i], 1 ); else map.replace(arr[i], map.get(arr[i]) + 1 ); } // Stores sum of odd and even // frequent elements int sum_odd = 0 , sum_even = 0 ; // Traverse the map Set<Map.Entry<Integer, Integer>> hmap = map.entrySet(); for (Map.Entry<Integer, Integer> data:hmap) { int key = data.getKey(); int val = data.getValue(); // If frequency is odd if (val % 2 != 0 ) // Add sum of all occurrences of // current element to sum_odd sum_odd += (key) * (val); // If frequency is even if (val % 2 == 0 ) // Add sum of all occurrences of // current element to sum_even sum_even += (key) * (val); } // Calculate difference // between their sum int diff = sum_even - sum_odd; // Return diff return diff; } // Driver Code public static void main(String args[]) { int arr[] = { 1 , 5 , 5 , 2 , 4 , 3 , 3 }; int N = arr.length; System.out.println(findSum(arr, N)); } } // This code is contributed by jyoti369 |
Python3
# Python3 program to find absolute difference # between the sum of all odd frequenct and # even frequent elements in an array # Function to find the sum of all even # and odd frequent elements in an array def findSum(arr, N): # Stores the frequency of array elements mp = {} # Traverse the array for i in range ( 0 , N): # Update frequency of # current element if arr[i] in mp: mp[arr[i]] + = 1 else : mp[arr[i]] = 1 # Stores sum of odd and even # frequent elements sum_odd, sum_even = 0 , 0 # Traverse the map for itr in mp: # If frequency is odd if (mp[itr] % 2 ! = 0 ): # Add sum of all occurrences of # current element to sum_odd sum_odd + = (itr) * (mp[itr]) # If frequency is even if (mp[itr] % 2 = = 0 ): # Add sum of all occurrences of # current element to sum_even sum_even + = (itr) * (mp[itr]) # Calculate difference # between their sum diff = sum_even - sum_odd # Return diff return diff # Driver code arr = [ 1 , 5 , 5 , 2 , 4 , 3 , 3 ] N = len (arr) print (findSum(arr, N)) # This code is contributed by divyeshrabadiya07 |
C#
// C# program to find absolute difference // between the sum of all odd frequenct and // even frequent elements in an array using System; using System.Collections.Generic; class GFG { // Function to find the sum of all even // and odd frequent elements in an array static int findSum( int [] arr, int N) { // Stores the frequency of array elements Dictionary< int , int > mp = new Dictionary< int , int >(); // Traverse the array for ( int i = 0; i < N; i++) { // Update frequency of // current element if (mp.ContainsKey(arr[i])) { mp[arr[i]]++; } else { mp[arr[i]] = 1; } } // Stores sum of odd and even // frequent elements int sum_odd = 0, sum_even = 0; // Traverse the map foreach (KeyValuePair< int , int > itr in mp) { // If frequency is odd if (itr.Value % 2 != 0) // Add sum of all occurrences of // current element to sum_odd sum_odd += (itr.Key) * (itr.Value); // If frequency is even if (itr.Value % 2 == 0) // Add sum of all occurrences of // current element to sum_even sum_even += (itr.Key) * (itr.Value); } // Calculate difference // between their sum int diff = sum_even - sum_odd; // Return diff return diff; } // Driver code static void Main() { int [] arr = { 1, 5, 5, 2, 4, 3, 3 }; int N = arr.Length; Console.Write(findSum(arr, N)); } } // This code is contributed by divyesh072019. |
Javascript
<script> // JavaScript program to find absolute difference // between the sum of all odd frequenct and // even frequent elements in an array // Function to find the sum of all even // and odd frequent elements in an array function findSum(arr, N) { // Stores the frequency of array elements var mp = {}; for (let i =0; i<N;i++) mp[arr[i]] = 0; // Traverse the array for (let i = 0; i < N; i++) { // Update frequency of // current element mp[arr[i]]++; } // Stores sum of odd and even // frequent elements var sum_odd = 0, sum_even = 0; // Traverse the map for (let itr in mp) { // If frequency is odd if (mp[itr] % 2 != 0) { // Add sum of all occurrences of // current element to sum_odd sum_odd += (itr) * (mp[itr]); } // If frequency is even if (mp[itr] % 2 == 0) { // Add sum of all occurrences of // current element to sum_even sum_even += (itr) * (mp[itr]); } } // Calculate difference // between their sum var diff = sum_even - sum_odd; // Return diff return diff; } // Driver Code var arr = new Array( 1, 5, 5, 2, 4, 3, 3 ); var N = arr.length; console.log( findSum(arr, N) ); // This code is contributed by ukasp. </script> |
Output:
9
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...