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

📅  最后修改于: 2021-09-02 07:38:05             🧑  作者: Mango

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

例子:

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

  • 初始化一个变量,比如X0 ,以存储X的结果值。
  • 初始化一个数组,比如大小为30 的count[] ,以在每个i索引处存储i设置的数组元素的数量。
  • 遍历给定数组,如果设置了i 位,则将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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live