 GFG App
Open App Browser
Continue

# Maximize bitwise AND of Array by changing at most K bits of elements

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++

```// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Function to find the maximum possible AND
// of array after performing at most K operations
int max_And(int N, int K, vector<int>& arr)
{
// Declare a vector bits_Count
vector<int> bits_Count(31);
for (int i = 0; i < N; i++) {
for (int j = 0; j < 31; j++) {

// storing the set bit count at
// j-th position
if ((arr[i] & (1 << j)) != 0)
bits_Count[j]++;
}
}

for (int i = 30; i >= 0; i--) {

// If possible to set the ith bit
// in all the present array elements
if (K >= N - bits_Count[i]) {
K -= (N - bits_Count[i]);
bits_Count[i] = N;
}
}

// Initialize max_Ans = 0
int max_Ans = 0;

// Loop to find the bits
// that will be set in the answer and
// calculate the maximum possible value
for (int i = 30; i >= 0; i--) {
if (bits_Count[i] == N) {
max_Ans += (1 << i);
}
}

// Return the maximum possible AND
return max_Ans;
}

// Driver Code
int main()
{
int K = 2;
vector<int> arr = { 1, 2, 3 };
int N = arr.size();

// Function Call
cout << max_And(N, K, arr) << endl;
return 0;
}```

## Java

```// Java implementation
import java.io.*;

class GFG {

// Function to find the maximum possible AND
// of array after performing at most K operations
public static int max_And(int N, int K, int[] arr)
{
// Declare a vector bits_Count
int[] bits_Count = new int;

for (int i = 0; i < N; i++) {
for (int j = 0; j < 31; j++) {

// storing the set bit count at
// j-th position
if ((arr[i] & (1 << j)) != 0)
bits_Count[j]++;
}
}

for (int i = 30; i >= 0; i--) {

// If possible to set the ith bit
// in all the present array elements
if (K >= N - bits_Count[i]) {
K -= (N - bits_Count[i]);
bits_Count[i] = N;
}
}

// Initialize max_Ans = 0
int max_Ans = 0;

// Loop to find the bits
// that will be set in the answer and
// calculate the maximum possible value
for (int i = 30; i >= 0; i--) {
if (bits_Count[i] == N) {
max_Ans += (1 << i);
}
}

// Return the maximum possible AND
return max_Ans;
}

public static void main(String[] args)
{
int K = 2;
int[] arr = { 1, 2, 3 };
int N = arr.length;

// Function Call
System.out.println(max_And(N, K, arr));
}
}

// This code is contributed by lokeshmvs21.```

## Python3

```# Python code to implement the approach

# Function to find the maximum possible AND
# of array after performing at most K operations
def max_And(N, K, arr):

# Declare a vector bits_Count
bits_Count = *31
for i in range(0, N):
for j in range(0, 31):

# storing the set bit count at
# j-th position
if ((arr[i] & (1 << j)) != 0):
bits_Count[j] += 1
for i in range(30, -1, -1):

# If possible to set the ith bit
# in all the present array elements
if (K >= N - bits_Count[i]):
K -= (N - bits_Count[i])
bits_Count[i] = N

# Initialize max_Ans = 0
max_Ans = 0

# Loop to find the bits
# that will be set in the answer and
# calculate the maximum possible value
for i in range(30, -1, -1):
if (bits_Count[i] == N):
max_Ans += (1 << i)

# Return the maximum possible AND
return max_Ans

# Driver Code
K = 2
arr = [1, 2, 3]
N = len(arr)

# Function Call
print(max_And(N, K, arr))

# this code is contributed by ksam24000
```

## C#

```// C# implementation
using System;

public class GFG{

// Function to find the maximum possible AND
// of array after performing at most K operations
public static int max_And(int N, int K, int[] arr)
{

// Declare a vector bits_Count
int[] bits_Count=new int;

for (int i = 0; i < N; i++) {
for (int j = 0; j < 31; j++) {

// storing the set bit count at
// j-th position
if ((arr[i] & (1 << j)) != 0)
bits_Count[j]++;
}
}

for (int i = 30; i >= 0; i--) {

// If possible to set the ith bit
// in all the present array elements
if (K >= N - bits_Count[i]) {
K -= (N - bits_Count[i]);
bits_Count[i] = N;
}
}

// Initialize max_Ans = 0
int max_Ans = 0;

// Loop to find the bits
// that will be set in the answer and
// calculate the maximum possible value
for (int i = 30; i >= 0; i--) {
if (bits_Count[i] == N) {
max_Ans += (1 << i);
}
}

// Return the maximum possible AND
return max_Ans;
}
static public void Main (){
int K = 2;
int[] arr = { 1, 2, 3 };
int N = arr.Length;

// Function Call
Console.WriteLine(max_And(N, K, arr));
}
}

// this code is contributed by ksam24000```

## Javascript

```    <script>
// JavaScript code to implement the approach

// Function to find the maximum possible AND
// of array after performing at most K operations
const max_And = (N, K, arr) => {
// Declare a vector bits_Count
let bits_Count = new Array(31).fill(0);
for (let i = 0; i < N; i++) {
for (let j = 0; j < 31; j++) {

// storing the set bit count at
// j-th position
if ((arr[i] & (1 << j)) != 0)
bits_Count[j]++;
}
}

for (let i = 30; i >= 0; i--) {

// If possible to set the ith bit
// in all the present array elements
if (K >= N - bits_Count[i]) {
K -= (N - bits_Count[i]);
bits_Count[i] = N;
}
}

// Initialize max_Ans = 0
let max_Ans = 0;

// Loop to find the bits
// that will be set in the answer and
// calculate the maximum possible value
for (let i = 30; i >= 0; i--) {
if (bits_Count[i] == N) {
max_Ans += (1 << i);
}
}

// Return the maximum possible AND
return max_Ans;
}

// Driver Code
let K = 2;
let arr = [1, 2, 3];
let N = arr.length;

// Function Call
document.write(max_And(N, K, arr));

// This code is contributed by rakeshsahni

</script>```
Output

`3`

Time Complexity: O(N * 32)
Auxiliary Space: O(1) because using only an extra array of size 32, which is constant

My Personal Notes arrow_drop_up