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++
// 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[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;
}
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 = [0]*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[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;
}
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>
Time Complexity: O(N * 32)
Auxiliary Space: O(1) because using only an extra array of size 32, which is constant
Similar Reads
Maximize Bitwise AND of Array by replacing at most one element Given an array arr[] containing N positive integers, the task is to maximize bitwise AND of the arr[] by picking at most one element of the arr[] and increment or decrement it by any value. Examples: Input: arr[] = {1, 2, 3}Output: 2Explanation: Following are the operations performed to maximize the
5 min read
Maximize Bitwise OR of Array by incrementing elements by at most K Given an array arr[], and an integer K, the task is to maximize the bitwise OR of the array arr[], where each element of arr[] can be incremented by almost K. Examples: Input: arr[]= {1, 3, 7, 0, 6, 1}, K = 2Output: [1 3 8 0 6 1]Explanation: Increase the number 7 by 1 ie 8 after that or of the array
7 min read
Minimize the max of Array by doing at most K increment and decrement Given a positive array arr[] of size N (2 ⤠N ⤠105) and a positive integer K, the task is to minimize the maximum value of arr[] by performing at most K operations where in each operation, you can select any two distinct integers i and j (0 ⤠i, j < N), then increase the value of arr[i] by 1 and
9 min read
Minimize the max of Array by breaking array elements at most K times Given an integer array arr[] of size N and a positive integer K, the task is to minimize the maximum of the array by replacing any element arr[i] into two positive elements (X, Y) at most K times such that arr[i] = X + Y. Examples: Input: arr = {9}, K = 2Output: 3Explanation: Operation 1: Replace el
15+ min read
Maximize the count of distinct elements in Array after at most K changes Given an array arr[], the task is to find the maximum number of distinct numbers in arr after at most K changes. In each change pick any element X from arr and change it to Y such that L <= Y <= R. Examples: Input: arr[] = {1, 2, 1, 4, 6, 4, 4}, L = 1, R = 5 and K = 2Output: 6Explanation: Foll
8 min read
Maximize equal elements in two Arrays after at most K increments Given two arrays arr1[] and arr2[] of length N each and an integer K, The task is to maximize the number of equal elements at the same index in arr1[] and arr2[] by incrementing any element of arr2[] but the total increment must be at most K. Examples: Input: arr1[] = {4, 5, 6, 7}, arr2[] = {3, 4, 5
6 min read