📌  相关文章
📜  设置位计数唯一的数组元素的总和

📅  最后修改于: 2021-10-27 16:42:00             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是找到数组中具有不同设置位计数的所有数组元素的总和。

例子:

方法:想法是将具有相应设置位计数的元素存储在map 上,然后找到具有唯一设置位计数的元素的总和。请按照以下步骤解决问题:

  • 初始化一个变量,比如sum来存储元素的结果总和,以及一个 Map,比如M ,它存储具有特定设置位计数的元素。
  • 遍历数组arr[]并根据 Map M 中设置的位计数存储元素arr[i]
  • 现在,遍历映射,如果任何设置位计数的频率为1,则将与其关联的相应值添加到变量sum 中
  • 完成上述步骤后,打印sum的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the approach
#include 
#include 
using namespace std;
 
// Function to count the number
// of set bits in an integer N
int setBitCount(int n)
{
     
    // Stores the count of set bits
    int ans = 0;
     
    // Iterate until N is non-zero
    while (n)
    {
        ans += n & 1;
        n >>= 1;
    }
     
    // Stores the resultant count
    return ans;
}
 
// Function to calculate sum of all array
// elements whose count of set bits are unique
int getSum(int *arr, int n)
{
     
    // Stores frequency of all possible
    // count of set bits
    map mp;
     
    // Stores the sum of array elements
    int ans = 0;
     
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // Count the number of set bits
        int key = setBitCount(arr[i]);
        mp[key] += 1;
    }
     
    // Traverse the array
    // And Update the value of ans
    for(int i = 0; i < n; i++)
    {
        int key = setBitCount(arr[i]);
         
        // If frequency is 1
        if (mp[key] == 1)
            ans += arr[i];
    }
    cout << ans;
}
 
// Driver Code
int main()
{
    int arr[5] = {8, 3, 7, 5, 3};
    int n = sizeof(arr) / sizeof(arr[0]);
     
    getSum(arr, n);
     
    return 0;
}
 
// This code is contributed by rohitsingh07052


Java
// Java program for the approach
import java.util.*;
class GFG
{
 
  // Function to count the number
  // of set bits in an integer N
  static int setBitCount(int n)
  {
 
    // Stores the count of set bits
    int ans = 0;
 
    // Iterate until N is non-zero
    while (n != 0)
    {
      ans += n & 1;
      n >>= 1;
    }
 
    // Stores the resultant count
    return ans;
  }
 
  // Function to calculate sum of all array
  // elements whose count of set bits are unique
  static void getSum(int []arr, int n)
  {
 
    // Stores frequency of all possible
    // count of set bits
    Map mp = new HashMap();
 
    // Stores the sum of array elements
    int ans = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
 
      // Count the number of set bits
      int key = setBitCount(arr[i]);
      if(mp.containsKey(key))
        mp.put(key,mp.get(key) + 1);
      else
        mp.put(key, 1);
 
    }
 
    // Traverse the array
    // And Update the value of ans
    for(int i = 0; i < n; i++)
    {
      int key = setBitCount(arr[i]);
 
      // If frequency is 1
      if (mp.containsKey(key) && mp.get(key) == 1)
        ans += arr[i];
    }
    System.out.print(ans);
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int []arr = {8, 3, 7, 5, 3};
    int n = arr.length;
 
    getSum(arr, n);
  }
}
 
// This code is contributed by SURENDRA_GANGWAR.


Python3
# Python3 program for the approach
 
# Function to count the number
# of set bits in an integer N
def setBitCount(n):
   
    # Stores the count of set bits
    ans = 0
     
    # Iterate until N is non-zero
    while n:
 
        ans += n & 1
        n >>= 1
         
    # Stores the resultant count
    return ans
 
 
# Function to calculate sum of all array
# elements whose count of set bits are unique
def getSum(arr):
   
    # Stores frequency of all possible
    # count of set bits
    mp = {}
     
    # Stores the sum of array elements
    ans = 0
     
    # Traverse the array
    for i in arr:
       
        # Count the number of set bits
        key = setBitCount(i)
        mp[key] = [0, i]
 
    # Traverse the array
    for i in arr:
        key = setBitCount(i)
        mp[key][0] += 1
     
    # Update the value of ans
    for i in mp:
       
        # If frequency is 1
        if mp[i][0] == 1:
            ans += mp[i][1]
 
    print(ans)
 
# Driver Code
 
arr = [8, 3, 7, 5, 3]
getSum(arr)


C#
// C# program for the approach
using System;
using System.Collections.Generic;
 
class solution{
   
// Function to count the number
// of set bits in an integer N
static int setBitCount(int n)
{
     
    // Stores the count of set bits
    int ans = 0;
     
    // Iterate until N is non-zero
    while (n>0)
    {
        ans += n & 1;
        n >>= 1;
    }
     
    // Stores the resultant count
    return ans;
}
 
// Function to calculate sum of all array
// elements whose count of set bits are unique
static void getSum(int []arr, int n)
{
     
    // Stores frequency of all possible
    // count of set bits
    Dictionary mp = new Dictionary();
     
    // Stores the sum of array elements
    int ans = 0;
     
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // Count the number of set bits
        int key = setBitCount(arr[i]);
        if(mp.ContainsKey(key))
          mp[key] += 1;
        else
          mp[key] = 1;
    }
     
    // Traverse the array
    // And Update the value of ans
    for(int i = 0; i < n; i++)
    {
        int key = setBitCount(arr[i]);
         
        // If frequency is 1
        if (mp[key] == 1)
            ans += arr[i];
    }
   Console.Write(ans);
}
 
// Driver Code
public static void Main()
{
    int []arr = {8, 3, 7, 5, 3};
    int n = arr.Length;
     
    getSum(arr, n);
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
15

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程