📌  相关文章
📜  从给定数组中删除最多 K 个元素后的最小按位或

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

从给定数组中删除最多 K 个元素后的最小按位或

给定一个长度为N的数组A[] ,任务是在删除最多K个元素后找到元素的按位或的最小可能值。

例子:

方法:这个问题可以根据以下思路使用位操作来解决:

请按照下面给出的插图更好地理解。

插图 :

按照下面提到的步骤来实现上述观察:

  • 创建一个哈希数组
  • 遍历给定数组并将每个元素的 MSB 索引递增到哈希数组中。
  • 现在,从 MSB 和每次迭代中遍历哈希数组:
    • 检查是否可以删除所有第 i位为 MSB 的元素,即 setBitCount ≤ K。
    • 如果 setBitCount ≤ K,则不计算该元素的 OR,只需减小 K。
    • 如果不是,则计算第 i位为 MSB 的元素的 OR。
  • 删除 K 个元素后返回计算 OR。

下面是上述方法的实现:

C++
// C++ program to find Minimum bitwise OR
// after removing at most K elements.
 
#include 
using namespace std;
 
// Function to find the minimum possible OR
int minimumOR(vector A, int N, int K)
{
    vector SetBitCount(32, 0);
    int OR = 0, FirstSetBit = 0;
 
    // Sort in reverse order
    sort(A.rbegin(), A.rend());
    for (int i = 0; i < N; i++) {
        FirstSetBit = log2(A[i]) + 1;
        SetBitCount[FirstSetBit]++;
    }
 
    // Traverse from MSB to LSB
    for (int i = 31, j = 0; i >= 0; i--) {
        if (SetBitCount[i] <= K) {
            K -= SetBitCount[i];
            j += SetBitCount[i];
        }
        else {
            for (int x = j;
                 j < x + SetBitCount[i]; j++)
                OR = OR | A[j];
        }
    }
 
    return OR;
}
 
// Driver code
int main()
{
    int N = 7;
    vector A = { 1, 10, 9, 4, 5, 16, 8 };
    int K = 3;
 
    cout << minimumOR(A, N, K);
    return 0;
}


Java
// Java program to find Minimum bitwise OR
// after removing at most K elements.
import java.util.*;
public class GFG
{
 
  // Function to reverse an array
  static void reverse(int a[], int n)
  {
    int i, k, t;
    for (i = 0; i < n / 2; i++) {
      t = a[i];
      a[i] = a[n - i - 1];
      a[n - i - 1] = t;
    }
  }
 
  // Function to find the minimum possible OR
  static int minimumOR(int[] A, int N, int K)
  {
    int[] SetBitCount = new int[32];
    for (int i = 0; i < 32; i++) {
      SetBitCount[i] = 0;
    }
    int OR = 0, FirstSetBit = 0;
 
    // Sort array in ascending order.
    Arrays.sort(A);
    // reverse array
    reverse(A, N);
 
    for (int i = 0; i < N; i++) {
      FirstSetBit
        = (int)(Math.log(A[i]) / Math.log(2)) + 1;
      SetBitCount[FirstSetBit]++;
    }
 
    // Traverse from MSB to LSB
    for (int i = 31, j = 0; i >= 0; i--) {
      if (SetBitCount[i] <= K) {
        K -= SetBitCount[i];
        j += SetBitCount[i];
      }
      else {
        for (int x = j; j < x + SetBitCount[i]; j++)
          OR = (OR | A[j]);
      }
    }
 
    return OR;
  }
 
  // Driver code
  public static void main(String args[])
  {
    int N = 7;
    int[] A = { 1, 10, 9, 4, 5, 16, 8 };
    int K = 3;
 
    System.out.println(minimumOR(A, N, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python
# Python program to find Minimum bitwise OR
# after removing at most K elements.
import math
 
# Function to find the minimum possible OR
def minimumOR(A, N, K):
 
    SetBitCount = [0] * 32
    OR = 0
    FirstSetBit = 0
 
    # Sort in reverse order
    A.sort(reverse=True)
 
    for i in range(0, N):
 
        FirstSetBit = int(math.log(A[i], 2)) + 1
        SetBitCount[FirstSetBit] += 1
 
    # Traverse from MSB to LSB
    j = 0
    i = 31
    while(i >= 0):
        if (SetBitCount[i] <= K):
            K -= SetBitCount[i]
            j += SetBitCount[i]
 
        else:
            x = j
            while(j < x + SetBitCount[i]):
                OR = OR | A[j]
                j += 1
 
        i -= 1
 
    return OR
 
# Driver code
N = 7
A = [1, 10, 9, 4, 5, 16, 8]
K = 3
 
print(minimumOR(A, N, K))
 
# This code is contributed by Samim Hossainn Mondal.


C#
// C# program to find Minimum bitwise OR
// after removing at most K elements.
using System;
class GFG {
 
  // Function to find the minimum possible OR
  static int minimumOR(int[] A, int N, int K)
  {
    int[] SetBitCount = new int[32];
    for (int i = 0; i < 32; i++) {
      SetBitCount[i] = 0;
    }
    int OR = 0, FirstSetBit = 0;
 
    // Sort array in ascending order.
    Array.Sort(A);
    // reverse array
    Array.Reverse(A);
 
    for (int i = 0; i < N; i++) {
      FirstSetBit = (int)Math.Log(A[i], 2) + 1;
      SetBitCount[FirstSetBit]++;
    }
 
    // Traverse from MSB to LSB
    for (int i = 31, j = 0; i >= 0; i--) {
      if (SetBitCount[i] <= K) {
        K -= SetBitCount[i];
        j += SetBitCount[i];
      }
      else {
        for (int x = j; j < x + SetBitCount[i]; j++)
          OR = (OR | A[j]);
      }
    }
 
    return OR;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 7;
    int[] A = { 1, 10, 9, 4, 5, 16, 8 };
    int K = 3;
 
    Console.Write(minimumOR(A, N, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
11

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