Minimize replacements of Array elements to make bitwise AND greater than K
Given an array A[] of N integers and an integer K, the task is to find the minimum number of replacements of array elements required such that the bitwise AND of all array elements is strictly greater than K.
Examples:
Input: N = 4, K = 2, A[] = {3, 1, 2, 7}
Output: 2
Explanation: Change A[1] = 3 and A[2] = 11.
After performing two operations: Modified array: {3, 3, 11, 7}
Now, Bitwise AND of all the elements is 3 & 3 & 11 & 7 = 3
Input: N = 3, K = 1, A[] = {2, 2, 2}
Output: 0
Approach: This problem can be solved using Bit Manipulation as per the following idea:
Find the bit representation of the value K. For each bit assume that is set and calculate how many replacements are required to achieve that value as bitwise AND of array elements. The minimum among these values is the required answer.
Follow the below steps to implement the above observation:
- Create a mask variable with 32 bit and initialize it to zero.
- Starting from the 31st most significant bit keep checking if the ith bit in K is set or not.
- If the ith bit is set then set that bit in the mask also.
- If the bit is not set then create a temporary variable (say temp) having the value of mask but having the ith bit set so that bitwise AND of the array can be strictly greater than K.
- For each element in the array check if the bitwise AND of the element and temp is equal to temp.
- If it is temp then there is no need to change this element of the array.
- Find the number of replacements (say X) by subtracting the count of elements not requiring change from N (the size of the array).
- The minimum value of X among all iterations from all 31st bit to 0th bit is the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int count( int N, vector< int > A, int K)
{
int ans = N;
int mask = 0;
for ( int i = 31; i >= 0; i--) {
if ((K >> i) & 1)
mask |= (1 << i);
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;
}
int main()
{
int N = 4, K = 2;
vector< int > A = { 3, 1, 2, 7 };
int ans = count(N, A, K);
cout << ans << endl;
return 0;
}
|
C
#include<stdio.h>
#include<math.h>
int min( int a, int b)
{
return a<b?a:b;
}
int count( int N, int A[], int K)
{
int ans = N;
int mask = 0;
for ( int i = 31; i >= 0; i--) {
if ((K >> i) & 1)
mask |= (1 << i);
else {
int good = 0;
int temp = mask | (1 << i);
for ( int i=0;i<N;i++)
if ((A[i] & temp) == temp)
good++;
ans = min(ans, N - good);
}
}
return ans;
}
int main()
{
int N = 4, K = 2;
int A[] = {3,1,2,7};
int ans = count(N, A, K);
printf ( "Ans : %d" ,ans);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int count( int N, int [] A, int K)
{
int ans = N;
int mask = 0 ;
for ( int i = 31 ; i >= 0 ; i--) {
if (((K >> i) & 1 ) != 0 )
mask |= ( 1 << i);
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;
}
public static void main (String[] args) {
int N = 4 , K = 2 ;
int A[] = { 3 , 1 , 2 , 7 };
int ans = count(N, A, K);
System.out.println(ans);
}
}
|
Python
def count(N, A, K):
ans = N
mask = 0
i = 31
while (i > = 0 ):
if ((K >> i) & 1 ):
mask | = ( 1 << i)
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
N = 4
K = 2
A = [ 3 , 1 , 2 , 7 ]
ans = count(N, A, K)
print (ans)
|
C#
using System;
class GFG {
static int count( int N, int [] A, int K)
{
int ans = N;
int mask = 0;
for ( int i = 31; i >= 0; i--) {
if (((K >> i) & 1) != 0)
mask |= (1 << i);
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;
}
public static void Main()
{
int N = 4, K = 2;
int [] A = { 3, 1, 2, 7 };
int ans = count(N, A, K);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function count(N, A, K) {
let ans = N;
let mask = 0;
for (let i = 31; i >= 0; i--) {
if ((K >> i) & 1)
mask |= (1 << i);
else {
let good = 0;
let temp = mask | (1 << i);
for (let element of A)
if ((element & temp) == temp)
good++;
ans = Math.min(ans, N - good);
}
}
return ans;
}
let N = 4, K = 2;
let A = [3, 1, 2, 7];
let ans = count(N, A, K);
document.write(ans + '<br>' )
</script>
|
Output:
2
Time Complexity: O(N * LogM) where M is the maximum element of array.
Auxiliary Space: O(1)
Last Updated :
18 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...