Zeroing out Array with bitwise operations
Last Updated :
25 Oct, 2023
Given an array A of N elements. Your task is to output “YES” if you can make all the array elements zero by performing an infinite number of moves where in one move:
- Choose any two distinct indexes i and j from the array A. Evaluate the bitwise AND of A[i] & A[j]. (Let it be X)
- Replace A[i] with A[i] ^ X. (here ^ denotes bitwise XOR and X is the bitwise AND of A[i] & A[j])
- Replace A[j] with A[j] ^ X. (here ^ denotes bitwise XOR and X is the bitwise AND of A[i] & A[j]).
Examples:
Input: N = 4, A[] = [2, 3, 4, 5]
Output: YES
Explanation: We can do the following moves: (1, 2), (2, 4), (3, 4). (1-indexed)
- After the first move, the array looks like: [0, 1, 4, 5]
- After the second move, the array looks like: [0, 0, 4, 4]
- After the third move, the array looks like: [0, 0, 0, 0]
Input: N = 5, A[] = [1, 1, 1, 1, 1]
Sample Output: NO
Explanation: No matter how many operations you perform, we can’t make all elements of the array equal to ZERO.
Approach: This can be solved with the following idea:
- It is clear from the problem statement that we need to play with bits of the numbers. So let’s analyze what exactly a single move is doing.
- Let’s say A & B = X, here X will have set bits that are common in A and B. (It’s clearly evident from the truth table of AND)
- Now Let’s A ^ X = Y and B ^ X = Z, here Y and Z will have bits as unset that are common in A and B. (Because 1 ^ 1 = 0)
- So we can conclude that one operation leads to erasing of common bits between those two numbers. We erase the bits two at a time. Hence if any set bit has an odd frequency it will never be removed.
- So the final solution is to just check if all the bits at a particular position having an even frequency, if not, we can’t make zero.
Below are the steps involved in the implementation of the code:
- Start a for from 1 to 30 which indicates the position of the bits.
- For each particular position find out the number of set bits, which can be easily done as follows:
- (A[j] & (1 << i)) > 0, right shifting 1 by i bits and ANDing with A[j] which will give us whether the ith bit is set or not.
- If at any position if there are the odd number of set bits return “NO“. else check for all the positions if there are no odd set bits return “YES”.
Below is the implementation for the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
string solve( int A[], int N)
{
for ( int i = 0; i < 31; i++) {
int setBitsCount = 0;
for ( int j = 0; j < N; j++) {
if (A[j] & (1 << i)) {
setBitsCount += 1;
}
}
if (setBitsCount & 1)
return "NO" ;
}
return "YES" ;
}
int main()
{
int N = 4;
int A[] = { 2, 3, 4, 5 };
cout << solve(A, N);
}
|
Java
import java.util.Arrays;
class GFG {
static String solve( int [] A, int N)
{
for ( int i = 0 ; i < 31 ; i++) {
int setBitsCount = 0 ;
for ( int j = 0 ; j < N; j++) {
if ((A[j] & ( 1 << i)) != 0 ) {
setBitsCount += 1 ;
}
}
if ((setBitsCount & 1 ) != 0 )
return "NO" ;
}
return "YES" ;
}
public static void main(String[] args)
{
int N = 4 ;
int [] A = { 2 , 3 , 4 , 5 };
System.out.println(solve(A, N));
}
}
|
Python3
def solve(A, N):
for i in range ( 31 ):
set_bits_count = 0
for j in range (N):
if A[j] & ( 1 << i):
set_bits_count + = 1
if set_bits_count & 1 :
return "NO"
return "YES"
if __name__ = = "__main__" :
N = 4
A = [ 2 , 3 , 4 , 5 ]
print (solve(A, N))
|
C#
using System;
class GFG
{
static string Solve( int [] A, int N)
{
for ( int i = 0; i < 31; i++)
{
int setBitsCount = 0;
for ( int j = 0; j < N; j++)
{
if ((A[j] & (1 << i)) != 0)
{
setBitsCount += 1;
}
}
if ((setBitsCount & 1) != 0)
{
return "NO" ;
}
}
return "YES" ;
}
static void Main()
{
int N = 4;
int [] A = { 2, 3, 4, 5 };
Console.WriteLine(Solve(A, N));
}
}
|
Javascript
<script>
function solve(A, N) {
for (let i = 0; i < 31; i++) {
let setBitsCount = 0;
for (let j = 0; j < N; j++) {
if (A[j] & (1 << i)) {
setBitsCount += 1;
}
}
if (setBitsCount & 1) return "NO" ;
}
return "YES" ;
}
let N = 4;
let A = [2, 3, 4, 5];
document.write(solve(A, N));
</script>
|
Time complexity: O(N * 31)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...