给定一个整数数组arr[] ,任务是找到所有奇数频繁数组元素之和与所有偶数频繁数组元素之和之间的绝对差。
例子:
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
处理方法:按照以下步骤解决问题:
- 初始化一个 unordered_map 来存储数组元素的频率。
- 遍历数组并更新Map中数组元素的频率。
- 然后,遍历地图并将具有偶数频率的元素添加到变量中,例如sum_even ,将具有奇数频率的元素添加到另一个变量中,例如sum_odd 。
- 最后,打印sum_odd和sum_even之间的差异。
下面是上述方法的实现:
C++
// C++ program to find absolute difference
// between the sum of all odd frequenct and
// even frequent elements in an array
#include
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 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 map = new HashMap();
// 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> hmap = map.entrySet();
for(Map.Entry 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 mp = new Dictionary();
// 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 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.
输出:
9
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live