📌  相关文章
📜  具有K个设置位的最小元素,使得每个具有K个数组元素的按位与之和最大

📅  最后修改于: 2021-04-17 19:23:26             🧑  作者: Mango

给定由N个整数和整数K组成的数组arr [] ,任务是找到恰好有K个设置位的最小整数X ,以使每个数组元素arr [i]X的按位与之和最大。

例子:

方法:可以使用贪婪方法解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个变量,例如X0 ,以存储X的结果值。
  • 初始化一个数组,比如说count []的大小为30 ,以在每个i索引处存储具有第ibit设置的数字数组元素。
  • 遍历给定的数组,如果第i位置1 ,则将count [i]更新1
  • 初始化一个对对的向量,例如ans ,以存储每个位和值的贡献,即,如果设置了i位,则将值{i,count [i] 2 i }存储Ans中
  • 按第二个元素的降序对对向量进行排序。
  • [0,K – 1]范围内遍历向量Ans ,并将X的值更新为X2的按位或(Ans [i] .first)
  • 完成上述步骤后,打印X的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Comparator function to sort the
// vector of pairs
bool comp(pair& a,
          pair& b)
{
    // If the second is not the same
    // then sort in decreasing order
    if (a.second != b.second)
        return a.second > b.second;
 
    // Otherwise
    return a.first < b.first;
}
 
// Function to find the value of X
// such that Bitwise AND of all array
// elements with X is maximum
int maximizeSum(int arr[], int n, int k)
{
    // Stores the count of set bit at
    // each position
    vector cnt(30, 0);
 
    // Stores the resultant value of X
    int X = 0;
 
    // Calculate the count of set bits
    // at each position
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 30; j++) {
 
            // If the jth bit is set
            if (arr[i] & (1 << j))
                cnt[j]++;
        }
    }
 
    // Stores the contribution
    // of each set bit
    vector > v;
 
    // Store all bit and amount of
    // contribution
    for (int i = 0; i < 30; i++) {
 
        // Find the total contribution
        int gain = cnt[i] * (1 << i);
        v.push_back({ i, gain });
    }
 
    // Sort V[] in decreasing
    // order of second parameter
    sort(v.begin(), v.end(), comp);
 
    // Choose exaclty K set bits
    for (int i = 0; i < k; i++) {
        X |= (1 << v[i].first);
    }
 
    // Print the answer
    cout << X;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 4, 5, 1 };
    int K = 1;
    int N = sizeof(arr) / sizeof(arr[0]);
    maximizeSum(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the value of X
// such that Bitwise AND of all array
// elements with X is maximum
static void maximizeSum(int arr[], int n, int k)
{
     
    // Stores the count of set bit at
    // each position
    int cnt[] = new int[30];
 
    // Stores the resultant value of X
    int X = 0;
 
    // Calculate the count of set bits
    // at each position
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < 30; j++)
        {
             
            // If the jth bit is set
            if ((arr[i] & (1 << j)) != 0)
                cnt[j]++;
        }
    }
 
    // Stores the contribution
    // of each set bit
    ArrayList v = new ArrayList<>();
 
    // Store all bit and amount of
    // contribution
    for(int i = 0; i < 30; i++)
    {
         
        // Find the total contribution
        int gain = cnt[i] * (1 << i);
        v.add(new int[] { i, gain });
    }
 
    // Sort V[] in decreasing
    // order of second parameter
    Collections.sort(v, (a, b) -> {
         
        // If the second is not the same
        // then sort in decreasing order
        if (a[1] != b[1])
            return b[1] - a[1];
 
        // Otherwise
        return a[0] - b[0];
    });
 
    // Choose exaclty K set bits
    for(int i = 0; i < k; i++)
    {
        X |= (1 << v.get(i)[0]);
    }
 
    // Print the answer
    System.out.println(X);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 4, 5, 1 };
    int K = 1;
    int N = arr.length;
     
    maximizeSum(arr, N, K);
}
}
 
// This code is contributed by Kingash


输出:
4

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