📌  相关文章
📜  数组中奇数和偶数元素之和之间的差

📅  最后修改于: 2021-05-17 06:16:31             🧑  作者: Mango

给定一个整数数组arr [] ,任务是找到所有奇数频繁数组元素之和与所有偶数频繁数组元素之和之间的绝对差。

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个unordered_map以存储数组元素的频率。
  • 遍历数组并更新Map中数组元素的频率。
  • 然后,遍历该图,将偶数频率的元素添加到一个变量,例如sum_even ,而奇数频率的元素添加到另一个变量,例如sum_odd
  • 最后,打印sum_oddsum_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)