📌  相关文章
📜  通过将相同位数的数字相加得到的最大和

📅  最后修改于: 2021-04-27 20:02:26             🧑  作者: Mango

给定一个由N个数字组成的数组,任务是找到可以通过将具有相同设置位数的数字相加而获得的最大和。

例子:

方法

  • 遍历数组并计算每个元素的设置位数。
  • 初始化一个32位的数组,假设该数字最多具有32个设置位。
  • 循环访问数组,并将数组元素添加到指示设置位数的位置。
  • 遍历并找到最大和然后返回。

下面是上述方法的实现:

C++
// C++ program to find maximum sum
// by adding numbers with same number of set bits
#include 
using namespace std;
  
// count the number of bits
// for each element of array
int bit_count(int n)
{
    int count = 0;
  
    // Count the number of set bits
    while (n) {
        count++;
  
        n = n & (n - 1);
    }
  
    return count;
}
  
// Function to return the
// the maximum sum
int maxsum(int arr[], int n)
{
    int bits[n];
  
    // Calculate the
    for (int i = 0; i < n; i++) {
        bits[i] = bit_count(arr[i]);
    }
  
    // Assuming the number to be
    // a maximum of 32 bits
    int sum[32] = { 0 };
  
    // Add the number to the
    // number of set bits
    for (int i = 0; i < n; i++) {
        sum[bits[i]] += arr[i];
    }
  
    int maximum = 0;
  
    // Find the maximum sum
    for (int i = 0; i < 32; i++) {
  
        maximum = max(sum[i], maximum);
    }
  
    return maximum;
}
  
// Driver code
int main()
{
  
    int arr[] = { 2, 3, 8, 5, 6, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << maxsum(arr, n);
  
    return 0;
}


Java
// Java program to find maximum sum
// by adding numbers with same number of set bits
  
class GFG
{
// count the number of bits
// for each element of array
static int bit_count(int n)
{
    int count = 0;
  
    // Count the number of set bits
    while (n>0)
    {
        count++;
  
        n = n & (n - 1);
    }
  
    return count;
}
  
// Function to return the
// the maximum sum
static int maxsum(int[] arr, int n)
{
    int[] bits=new int[n];
  
    // Calculate the
    for (int i = 0; i < n; i++) 
    {
        bits[i] = bit_count(arr[i]);
    }
  
    // Assuming the number to be
    // a maximum of 32 bits
    int[] sum=new int[32];
  
    // Add the number to the
    // number of set bits
    for (int i = 0; i < n; i++)
    {
        sum[bits[i]] += arr[i];
    }
  
    int maximum = 0;
  
    // Find the maximum sum
    for (int i = 0; i < 32; i++)
    {
  
        maximum = Math.max(sum[i], maximum);
    }
  
    return maximum;
}
  
// Driver code
public static void main (String[] args)
{
    int[] arr = { 2 ,3 , 8, 5, 6, 7 };
    int n = arr.length;
    System.out.println(maxsum(arr, n));
  
}
}
  
// This Code is contributed by mits


Python3
# Python3 program to find maximum
# sum by adding numbers with
# same number of set bits
  
# count the number of bits
# for each element of array
def bit_count(n):
    count = 0;
  
    # Count the number
    # of set bits
    while (n > 0):
        count += 1;
  
        n = n & (n - 1);
  
    return count;
  
# Function to return the
# the maximum sum
def maxsum(arr, n):
    bits = [0] * n;
  
    # Calculate the
    for i in range(n): 
        bits[i] = bit_count(arr[i]);
  
    # Assuming the number to be
    # a maximum of 32 bits
    sum = [0] * 32;
  
    # Add the number to the
    # number of set bits
    for i in range(n): 
        sum[bits[i]] += arr[i];
  
    maximum = 0;
  
    # Find the maximum sum
    for i in range(32):
  
        maximum = max(sum[i], maximum);
  
    return maximum;
  
# Driver code
arr = [ 2, 3, 8, 5, 6, 7 ];
n = len(arr);
print(maxsum(arr, n));
  
# This code is contributed by mits


C#
// C# program to find maximum 
// sum by adding numbers with 
// same number of set bits
using System;
  
class GFG
{
// count the number of bits
// for each element of array
static int bit_count(int n)
{
    int count = 0;
  
    // Count the number
    // of set bits
    while (n > 0)
    {
        count++;
  
        n = n & (n - 1);
    }
  
    return count;
}
  
// Function to return the
// the maximum sum
static int maxsum(int[] arr, int n)
{
    int[] bits = new int[n];
  
    // Calculate the
    for (int i = 0; i < n; i++) 
    {
        bits[i] = bit_count(arr[i]);
    }
  
    // Assuming the number to be
    // a maximum of 32 bits
    int[] sum = new int[32];
  
    // Add the number to the
    // number of set bits
    for (int i = 0; i < n; i++)
    {
        sum[bits[i]] += arr[i];
    }
  
    int maximum = 0;
  
    // Find the maximum sum
    for (int i = 0; i < 32; i++)
    {
        maximum = Math.Max(sum[i], maximum);
    }
  
    return maximum;
}
  
// Driver code
static void Main()
{
    int[] arr = { 2 ,3 , 8, 5, 6, 7 };
    int n = arr.Length;
    Console.WriteLine(maxsum(arr, n));
}
}
  
// This Code is contributed by mits


PHP


输出:
14

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