Maximize bitwise AND of Array by changing at most K bits of elements
Last Updated :
01 Nov, 2022
Given an array arr[] of length N. You can perform at most K operations on the array of the following type:
- Choose an index i (0 ? i ? N-1) and set the j-th bit of arr[i] to 1 (0 ? j ? 30).
The task is to find the maximum possible value of bitwise AND of all array elements after performing at most K operations.
Examples:
Input: N = 3, K = 2, Arr = [1, 2, 3]
Output: 3
Explanation: Following is the 32 bit binary representation of 1, 2, 3.
1->00000000000000000000000000000001
2->00000000000000000000000000000010
3->00000000000000000000000000000011
So, set 0th bit of 2 and 1st bit of 1 which will require 2 operation.
Now the binary representation of the numbers will be
3->00000000000000000000000000000011
3->00000000000000000000000000000011
3->00000000000000000000000000000011
So, 3 & 3 & 3 = 3 .
Input: N = 7, K = 0, Arr = [4, 6, 6, 28, 6, 6, 12]
Output: 4
Explanation: Since K is 0 we cannot do any operation.
So the bitwise AND of the array is (4 & 6 & 6 & 28 & 6 & 6 & 12) = 4 .
Approach: The problem can be solved based on the following idea:
The bitwise AND will be larger when all the bits (as left as possible )of all the elements are set. So, set the most significant bit that is possible in all the elements of the array
Follow the below steps to solve the problem:
- Declare a vector bits_Count which will store the total count of set bits at the jth bit (0 ? j ? 30) of all the numbers of the array.
- Now Traverse the bits_Count from the reverse and check if K ? N – bits_Count[i] (0 ? i ? N-1)
- If the condition is satisfied, decrement K by N – bits_Count[i] and set bits_Count[i] = N.
- Declare a max_Ans variable and initialize it to 0.
- Now traverse the bits_Count array from i = 0 to 30:
- Check if bits_Count[i] = N, then increment max_Ans by 2i (0 ? i ? 30).
- Return max_Ans as the required answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int max_And( int N, int K, vector< int >& arr)
{
vector< int > bits_Count(31);
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < 31; j++) {
if ((arr[i] & (1 << j)) != 0)
bits_Count[j]++;
}
}
for ( int i = 30; i >= 0; i--) {
if (K >= N - bits_Count[i]) {
K -= (N - bits_Count[i]);
bits_Count[i] = N;
}
}
int max_Ans = 0;
for ( int i = 30; i >= 0; i--) {
if (bits_Count[i] == N) {
max_Ans += (1 << i);
}
}
return max_Ans;
}
int main()
{
int K = 2;
vector< int > arr = { 1, 2, 3 };
int N = arr.size();
cout << max_And(N, K, arr) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int max_And( int N, int K, int [] arr)
{
int [] bits_Count = new int [ 31 ];
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < 31 ; j++) {
if ((arr[i] & ( 1 << j)) != 0 )
bits_Count[j]++;
}
}
for ( int i = 30 ; i >= 0 ; i--) {
if (K >= N - bits_Count[i]) {
K -= (N - bits_Count[i]);
bits_Count[i] = N;
}
}
int max_Ans = 0 ;
for ( int i = 30 ; i >= 0 ; i--) {
if (bits_Count[i] == N) {
max_Ans += ( 1 << i);
}
}
return max_Ans;
}
public static void main(String[] args)
{
int K = 2 ;
int [] arr = { 1 , 2 , 3 };
int N = arr.length;
System.out.println(max_And(N, K, arr));
}
}
|
Python3
def max_And(N, K, arr):
bits_Count = [ 0 ] * 31
for i in range ( 0 , N):
for j in range ( 0 , 31 ):
if ((arr[i] & ( 1 << j)) ! = 0 ):
bits_Count[j] + = 1
for i in range ( 30 , - 1 , - 1 ):
if (K > = N - bits_Count[i]):
K - = (N - bits_Count[i])
bits_Count[i] = N
max_Ans = 0
for i in range ( 30 , - 1 , - 1 ):
if (bits_Count[i] = = N):
max_Ans + = ( 1 << i)
return max_Ans
K = 2
arr = [ 1 , 2 , 3 ]
N = len (arr)
print (max_And(N, K, arr))
|
C#
using System;
public class GFG{
public static int max_And( int N, int K, int [] arr)
{
int [] bits_Count= new int [31];
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < 31; j++) {
if ((arr[i] & (1 << j)) != 0)
bits_Count[j]++;
}
}
for ( int i = 30; i >= 0; i--) {
if (K >= N - bits_Count[i]) {
K -= (N - bits_Count[i]);
bits_Count[i] = N;
}
}
int max_Ans = 0;
for ( int i = 30; i >= 0; i--) {
if (bits_Count[i] == N) {
max_Ans += (1 << i);
}
}
return max_Ans;
}
static public void Main (){
int K = 2;
int [] arr = { 1, 2, 3 };
int N = arr.Length;
Console.WriteLine(max_And(N, K, arr));
}
}
|
Javascript
<script>
const max_And = (N, K, arr) => {
let bits_Count = new Array(31).fill(0);
for (let i = 0; i < N; i++) {
for (let j = 0; j < 31; j++) {
if ((arr[i] & (1 << j)) != 0)
bits_Count[j]++;
}
}
for (let i = 30; i >= 0; i--) {
if (K >= N - bits_Count[i]) {
K -= (N - bits_Count[i]);
bits_Count[i] = N;
}
}
let max_Ans = 0;
for (let i = 30; i >= 0; i--) {
if (bits_Count[i] == N) {
max_Ans += (1 << i);
}
}
return max_Ans;
}
let K = 2;
let arr = [1, 2, 3];
let N = arr.length;
document.write(max_And(N, K, arr));
</script>
|
Time Complexity: O(N * 32)
Auxiliary Space: O(1) because using only an extra array of size 32, which is constant
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...