Bitwise AND of the Array | Practice | GeeksforGeeks

Given an array A[ ] of N integers and an integer X. In one operation, you can change the ith element of the array to any integer value where 1 ≤ i ≤ N. Calculate minimum number of such operations required such that the bitwise AND of all the elements of the array is strictly greater than X.
Example 1:
Input:
N = 4, X = 2
A[] = {3, 1, 2, 7}
Output: 
2
Explanation: 
After performing two operations:
Modified array: {3, 3, 11, 7} 
Now, Bitwise AND of all the elements
is 3 & 3 & 11 & 7 = 3 

Example 2:
Input:
N = 3, X = 1
A[] = {2, 2, 2}
Output: 
0

Your Task:
You don't need to read input or print anything. Your task is to complete the function count( ) which takes N, A[ ] and X as input parameters and returns the minimum number of operations required.
Expected Time Complexity: O(N * Log(max(A[ ])))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 105
1 ≤ A[i] ≤ 109
1 ≤ X ≤ 109
-1
akathoniya523 months ago
class Solution {    int count(int N, int A[], int X)    {          // code here       int prefix=0,ans=N;       for(int i=30;i>=0;i--){           if(((X>>i)&1)!=0){               prefix^=(1l<<i);               continue;           }           int ct=0;           long p=prefix^(1l<<i);           for(int j=0;j<N;j++){               if((A[j]&p)==p)                   ct++;           }           ans=Math.min(ans,N-ct);       }       return ans;   }} 
0
ganeshmarmat
This comment was deleted.
0
reddysukesh27076 months ago
Are we not supposed to import classes in the solution (for answering the question in java)
0
rupasriachanta4216 months ago
Python solution :
 
Clickhere
-6
fungkwe6 months ago
Python code with explanation:
Firstly here is a little function to find the number of operations needed, if test is the bitwise AND of elements in A:
def numOp(test):
	count = 0
	for item in A:
		if test & item != test:
			count += 1
	return count
So the problem now becomes finding the list of inputs we should pass to this function, and then return the minimum among all results as the answer.
The inputs should satisfy the following conditions:
It is larger than X.We can skip all numbers which, as a binary number, contains some 1's but it can be replaced by 0's and still satisfy condition 1. (The inputs will have as few 1's as possible)It may have more than one 1's if, as a binary number, it has same number of digits as X. (Because we can't apply condition 2)
It is larger than X.
We can skip all numbers which, as a binary number, contains some 1's but it can be replaced by 0's and still satisfy condition 1. (The inputs will have as few 1's as possible)
It may have more than one 1's if, as a binary number, it has same number of digits as X. (Because we can't apply condition 2)
From the above condition we can obtain a formula for the list as:
X + b - X % b
,where b is 2^i for 0 <= i <= log(max(A)) 
For example if X is 18, the list of inputs will be 19, 20, 24, 32, 64, 128, 256...etc. We can stop iterating after the input gets greater than max(A), therefore the number of inputs we need to iterate will be log(max(A)). Thus this approach has the Expected Time Complexity O(N*log(max(A))).
0
whynesspower
This comment was deleted.
-7
whynesspower6 months ago
 
 
Here is the solution video-
https://youtu.be/--1uP_gcOCg
0
whynesspower6 months ago
+4
lapimpale6 months ago
Java Solution
 
int count(int N, int A[], int X) 
    {   
        // code here
        int prefix=0,ans=N;
        for(int i=30;i>=0;i--){
            if(((X>>i)&1)!=0){
                prefix^=(1l<<i);
                continue;
            }
            int ct=0;
            long p=prefix^(1l<<i);
            for(int j=0;j<N;j++){
                if((A[j]&p)==p)
                    ct++;
            }
            ans=Math.min(ans,N-ct);
        }
        return ans;
    }
+4
mastermind_6 months ago
C++ Shorter Solution,  Bits Manipulation + Greedy ~ O(32*N)
int count(int n, vector<int> a, int x) {
	int msb = __lg(x), ans = n, temp = 0, vis[n] = {0};
	for (int i = 30; i >= 0; i--) {
		int cnt = 0;
		for (int j = 0; j < n; j++) {
			if (!vis[j] && ((x >> i) & 1) && !((a[j] >> i) & 1) )temp++, vis[j] = 1;
			if (!((a[j] >> i) & 1) && !vis[j])cnt++;
		}
		if (!((x >> i) & 1))ans = min(ans, temp + cnt);
	}
	return ans;
}
Idea: Start filling higher bits first that is present in X and mark them as visited, if a bit is not present in X, then check is it's being already marked and not present in a[j], Keep saving the min answer.Try to understand the above code, it's a nice bits and observation based problem. Spend some time on it.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.