从给定数组中删除最多 K 个元素后的最小按位或
给定一个长度为N的数组A[] ,任务是在删除最多K个元素后找到元素的按位或的最小可能值。
例子:
Input: A = {1, 10, 9, 4, 5, 16, 8}, K = 3
Output: 11
Explanation: Remove 4, 5, 16 for the given array.
The remaining elements are {1, 10, 9, 5}.
The OR of these elements are 11 which is the minimum possible.
Input: A = {1, 2, 4, 8}, K = 1
Output: 7
Explanation: Remove 8 form the Array, Minimum OR= 1 | 2 | 4 = 7
方法:这个问题可以根据以下思路使用位操作来解决:
Try to remove the highest MSB elements first, then the elements having 2nd highest MSB and so on until K elements are removed.
请按照下面给出的插图更好地理解。
插图 :
Consider A[] = [1, 10, 9, 4, 5, 16, 8 ]
- Binary representation of the elements are:
1 – 00001
10 – 01010
9 – 01001
4 – 00100
5 – 00101
16 – 10000
8 – 01000 - The positions of the MSB of every elements are:
1 -> 0, 10 -> 3, 9 -> 3, 4 -> 2, 5 -> 2, 16 -> 4, 8 -> 3 - Therefore count of elements having MSB at ith position are as given below:
setBitCount = [1, 0, 2, 3, 1 ]
MSB position 0, 1, 2, 3, 4 - Traverse array and check if current setBitCount is less than k, then remove all elements with current Bit as FirstSetBit.
For, K = 3, traverse array:
=>When i = 4: setBitCount[i] = 1, which is less than K so remove 16, and now K = 2.
=>When i = 3: K < setBitCount[i] (i.e 3). So don’t remove it.
=>When i = 2: setBitCount[i] = 2 ≤ K, so remove 4 and 5. Now, K =0. - Calculate OR for all remaining elements i.e (1, 5, 9, 10) = 11
按照下面提到的步骤来实现上述观察:
- 创建一个哈希数组。
- 遍历给定数组并将每个元素的 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)