Difference between sum of odd and even frequent elements in an Array
Last Updated :
08 Mar, 2023
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:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int arr[], int N)
{
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[arr[i]]++;
}
int sum_odd = 0, sum_even = 0;
for ( auto itr = mp.begin();
itr != mp.end(); itr++) {
if (itr->second % 2 != 0)
sum_odd += (itr->first)
* (itr->second);
if (itr->second % 2 == 0)
sum_even += (itr->first)
* (itr->second);
}
int diff = sum_even - sum_odd;
return diff;
}
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
import java.util.*;
import java.io.*;
import java.math.*;
class GFG{
static int findSum( int arr[], int N)
{
Map<Integer,
Integer> map = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++)
{
if (!map.containsKey(arr[i]))
map.put(arr[i], 1 );
else
map.replace(arr[i], map.get(arr[i]) + 1 );
}
int sum_odd = 0 , sum_even = 0 ;
Set<Map.Entry<Integer, Integer>> hmap = map.entrySet();
for (Map.Entry<Integer, Integer> data:hmap)
{
int key = data.getKey();
int val = data.getValue();
if (val % 2 != 0 )
sum_odd += (key) * (val);
if (val % 2 == 0 )
sum_even += (key) * (val);
}
int diff = sum_even - sum_odd;
return diff;
}
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));
}
}
|
Python3
def findSum(arr, N):
mp = {}
for i in range ( 0 , N):
if arr[i] in mp:
mp[arr[i]] + = 1
else :
mp[arr[i]] = 1
sum_odd, sum_even = 0 , 0
for itr in mp:
if (mp[itr] % 2 ! = 0 ):
sum_odd + = (itr) * (mp[itr])
if (mp[itr] % 2 = = 0 ):
sum_even + = (itr) * (mp[itr])
diff = sum_even - sum_odd
return diff
arr = [ 1 , 5 , 5 , 2 , 4 , 3 , 3 ]
N = len (arr)
print (findSum(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int findSum( int [] arr, int N)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]]++;
}
else {
mp[arr[i]] = 1;
}
}
int sum_odd = 0, sum_even = 0;
foreach (KeyValuePair< int , int > itr in mp) {
if (itr.Value % 2 != 0)
sum_odd += (itr.Key)
* (itr.Value);
if (itr.Value % 2 == 0)
sum_even += (itr.Key)
* (itr.Value);
}
int diff = sum_even - sum_odd;
return diff;
}
static void Main()
{
int [] arr = { 1, 5, 5, 2, 4, 3, 3 };
int N = arr.Length;
Console.Write(findSum(arr, N));
}
}
|
Javascript
<script>
function findSum(arr, N)
{
var mp = {};
for (let i =0; i<N;i++)
mp[arr[i]] = 0;
for (let i = 0; i < N; i++) {
mp[arr[i]]++;
}
var sum_odd = 0, sum_even = 0;
for (let itr in mp) {
if (mp[itr] % 2 != 0) {
sum_odd += (itr)
* (mp[itr]);
}
if (mp[itr] % 2 == 0) {
sum_even += (itr)
* (mp[itr]);
}
}
var diff = sum_even - sum_odd;
return diff;
}
var arr = new Array( 1, 5, 5, 2, 4, 3, 3 );
var N = arr.length;
console.log( findSum(arr, N) );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach without using extra space:
- We can just sort the array
- After sorting all same time off elements will be adjacent to each other
- We can count the frequency of a element and add the element according to the odd or even frequency
Implementation:-
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int arr[], int N)
{
sort(arr,arr+N);
int freq=1,element=arr[0];
int odd_sum=0,even_sum=0;
for ( int i=1;i<N;i++)
{
if (arr[i]!=element)
{
if (freq%2){
odd_sum+=freq*element;
}
else {
even_sum+=freq*element;
}
freq=1;
element=arr[i];
}
else freq++;
}
if (freq%2){
odd_sum+=freq*element;
}
else {
even_sum+=freq*element;
}
return abs (odd_sum-even_sum);
}
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
import java.util.*;
public class Main {
static int findSum( int [] arr, int N)
{
Arrays.sort(arr);
int freq= 1 ,element=arr[ 0 ];
int odd_sum= 0 ,even_sum= 0 ;
for ( int i= 1 ;i<N;i++)
{
if (arr[i]!=element)
{
if (freq% 2 == 1 ){
odd_sum+=freq*element;
}
else {
even_sum+=freq*element;
}
freq= 1 ;
element=arr[i];
}
else freq++;
}
if (freq% 2 == 1 ){
odd_sum+=freq*element;
}
else {
even_sum+=freq*element;
}
return Math.abs(odd_sum-even_sum);
}
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));
}
}
|
Python3
def find_sum(arr, N):
arr.sort()
freq = 1
element = arr[ 0 ]
odd_sum = 0
even_sum = 0
for i in range ( 1 , N):
if arr[i] ! = element:
if freq % 2 :
odd_sum + = freq * element
else :
even_sum + = freq * element
freq = 1
element = arr[i]
else :
freq + = 1
if freq % 2 :
odd_sum + = freq * element
else :
even_sum + = freq * element
return abs (odd_sum - even_sum)
if __name__ = = "__main__" :
arr = [ 1 , 5 , 5 , 2 , 4 , 3 , 3 ]
N = len (arr)
print (find_sum(arr,N))
|
C#
using System;
public class Program
{
public static int FindSum( int [] arr, int N)
{
Array.Sort(arr);
int freq = 1, element = arr[0];
int odd_sum = 0, even_sum = 0;
for ( int i = 1; i < N; i++)
{
if (arr[i] != element)
{
if (freq % 2 != 0)
{
odd_sum += freq * element;
}
else
{
even_sum += freq * element;
}
freq = 1;
element = arr[i];
}
else
{
freq++;
}
}
if (freq % 2 != 0)
{
odd_sum += freq * element;
}
else
{
even_sum += freq * element;
}
return Math.Abs(odd_sum - even_sum);
}
public static void Main()
{
int [] arr = { 1, 5, 5, 2, 4, 3, 3 };
int N = arr.Length;
Console.WriteLine(FindSum(arr, N));
}
}
|
Javascript
function find_sum(arr, N) {
arr.sort();
let freq = 1;
let element = arr[0];
let odd_sum = 0;
let even_sum = 0;
for (let i = 1; i < N; i++) {
if (arr[i] !== element) {
if (freq % 2) {
odd_sum += freq * element;
} else {
even_sum += freq * element;
}
freq = 1;
element = arr[i];
} else {
freq++;
}
}
if (freq % 2) {
odd_sum += freq * element;
} else {
even_sum += freq * element;
}
return Math.abs(odd_sum - even_sum);
}
let arr = [1, 5, 5, 2, 4, 3, 3];
let N = arr.length;
console.log(find_sum(arr,N));
|
Time Complexity:- O(NlogN)
Auxiliary Space:- O(1)
Share your thoughts in the comments
Please Login to comment...