📌  相关文章
📜  最小化 Array 元素的替换以使按位与大于 K

📅  最后修改于: 2022-05-13 01:56:07.853000             🧑  作者: Mango

最小化 Array 元素的替换以使按位与大于 K

给定一个包含N个整数的数组A [] 和一个整数K ,任务是找到数组元素的最小替换次数,使得所有数组元素的按位与严格大于K

例子:

方法:这个问题可以通过位操作来解决,思路如下:

按照以下步骤实施上述观察:

  • 创建一个 32 位的掩码变量并将其初始化为零。
  • 从第 31最高有效位开始,继续检查 K 中的第 i位是否已设置。
    • 如果设置了第 i 个位,则还要在掩码中设置该位。
    • 如果未设置该位,则创建一个临时变量(例如temp ),其值为 mask 但设置第i位,以便数组的按位与可以严格大于K
    • 对于数组中的每个元素,检查元素和temp的按位与是否等于temp
      • 如果它是temp则无需更改数组的此元素。
    • 通过从 N (数组的大小)中减去不需要更改的元素的数量来找到替换的数量(比如X )。
  • 从第31位到0 位的所有迭代中X的最小值就是答案。

下面是上述方法的实现。

C++
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function to find the
// minimum number of replacements
int count(int N, vector A, int K)
{
    // Initially assuming we need to change
    // all elements of the array
    int ans = N;
 
    // Initialize mask as 0
    int mask = 0;
 
    // Starting from 31st bit check all
    // the bits of X if they are set or not
    for (int i = 31; i >= 0; i--) {
 
        // If bit is set,
        // set that bit in mask also
        if ((K >> i) & 1)
            mask |= (1 << i);
 
        // If bit is not set then
        // count the elements of array
        // requiring no change
        // and change answer to
        // minimum replacements.
        else {
            int good = 0;
            int temp = mask | (1 << i);
            for (auto element : A)
                if ((element & temp) == temp)
                    good++;
            ans = min(ans, N - good);
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    int N = 4, K = 2;
    vector A = { 3, 1, 2, 7 };
 
    // Function call
    int ans = count(N, A, K);
    cout << ans << endl;
    return 0;
}


Java
// Java code to implement the approach
import java.io.*;
 
class GFG {
 
  // Function to find the
  // minimum number of replacements
  static int count(int N, int[] A, int K)
  {
 
    // Initially assuming we need to change
    // all elements of the array
    int ans = N;
 
    // Initialize mask as 0
    int mask = 0;
 
    // Starting from 31st bit check all
    // the bits of X if they are set or not
    for (int i = 31; i >= 0; i--) {
 
      // If bit is set,
      // set that bit in mask also
      if (((K >> i) & 1) != 0)
        mask |= (1 << i);
 
      // If bit is not set then
      // count the elements of array
      // requiring no change
      // and change answer to
      // minimum replacements.
      else {
        int good = 0;
        int temp = mask | (1 << i);
        for( int element : A)
          if ((element & temp) == temp) good++;
        ans = Math.min(ans, N - good);
      }
    }
    return ans;
  }
 
  // Driver code
  public static void main (String[] args) {
    int N = 4, K = 2;
    int A[] = { 3, 1, 2, 7 };
 
    // Function call
    int ans = count(N, A, K);
    System.out.println(ans);
  }
}
 
// This code is contributed by hrithikgarg03188.


Python
# Python code to implement the approach
 
# Function to find the
# minimum number of replacements
def count(N, A, K):
 
    # Initially assuming we need to change
    # all elements of the array
    ans = N
 
    # Initialize mask as 0
    mask = 0
 
    # Starting from 31st bit check all
    # the bits of X if they are set or not
    i = 31
    while(i >= 0):
 
        # If bit is set,
        # set that bit in mask also
        if ((K >> i) & 1):
            mask |= (1 << i)
 
        # If bit is not set then
        # count the elements of array
        # requiring no change
        # and change answer to
        # minimum replacements.
        else:
            good = 0
            temp = mask | (1 << i)
            for x in range(0, len(A)):
                if ((A[x] & temp) == temp):
                    good += 1
            ans = min(ans, N - good)
 
        i -= 1
 
    return ans
 
# Driver code
N = 4
K = 2
A = [3, 1, 2, 7]
 
# Function call
ans = count(N, A, K)
print(ans)
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# code to implement the approach
using System;
class GFG {
 
  // Function to find the
  // minimum number of replacements
  static int count(int N, int[] A, int K)
  {
 
    // Initially assuming we need to change
    // all elements of the array
    int ans = N;
 
    // Initialize mask as 0
    int mask = 0;
 
    // Starting from 31st bit check all
    // the bits of X if they are set or not
    for (int i = 31; i >= 0; i--) {
 
      // If bit is set,
      // set that bit in mask also
      if (((K >> i) & 1) != 0)
        mask |= (1 << i);
 
      // If bit is not set then
      // count the elements of array
      // requiring no change
      // and change answer to
      // minimum replacements.
      else {
        int good = 0;
        int temp = mask | (1 << i);
        foreach(
          int element in A) if ((element & temp)
                                == temp) good++;
        ans = Math.Min(ans, N - good);
      }
    }
    return ans;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 4, K = 2;
    int[] A = { 3, 1, 2, 7 };
 
    // Function call
    int ans = count(N, A, K);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出:

2

时间复杂度: O(N * LogM) 其中 M 是数组的最大元素。
辅助空间: O(1)