Zeroing out Array with bitwise operations
Last Updated :
25 Oct, 2023
Improve
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++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to check whether all elements
// can be converted to zero
string solve(int A[], int N)
{
// Iterating for each bit position
for (int i = 0; i < 31; i++) {
// Variable to store the numbe of
// set bits at a particular
// position
int setBitsCount = 0;
for (int j = 0; j < N; j++) {
// Checking if ith bit is
// set or not
if (A[j] & (1 << i)) {
// Incrementing the count
setBitsCount += 1;
}
}
// Checking if number of set
// bits are odd
if (setBitsCount & 1)
return "NO";
}
return "YES";
}
// Driver code
int main()
{
int N = 4;
int A[] = { 2, 3, 4, 5 };
// Function call
cout << solve(A, N);
}
// JAVA code for the above approach:
import java.util.Arrays;
class GFG {
// Function to check whether all elements can be
// converted to zero
static String solve(int[] A, int N)
{
// Iterating for each bit position
for (int i = 0; i < 31; i++) {
// Variable to store the number of set bits at a
// particular position
int setBitsCount = 0;
for (int j = 0; j < N; j++) {
// Checking if ith bit is set or not
if ((A[j] & (1 << i)) != 0) {
// Incrementing the count
setBitsCount += 1;
}
}
// Checking if the number of set bits is odd
if ((setBitsCount & 1) != 0)
return "NO";
}
return "YES";
}
// Driver code
public static void main(String[] args)
{
int N = 4;
int[] A = { 2, 3, 4, 5 };
// Function call
System.out.println(solve(A, N));
}
}
// This code is contributed by shivamgupta310570
# Python code for the above approach
# Function to check whether all elements
# can be converted to zero
def solve(A, N):
# Iterating for each bit position
for i in range(31):
# Variable to store the number of
# set bits at a particular position
set_bits_count = 0
for j in range(N):
# Checking if ith bit is set or not
if A[j] & (1 << i):
# Incrementing the count
set_bits_count += 1
# Checking if the number of set bits is odd
if set_bits_count & 1:
return "NO"
return "YES"
# Driver code
if __name__ == "__main__":
N = 4
A = [2, 3, 4, 5]
# Function call
print(solve(A, N))
# This code is contributed by Susobhan Akhuli
// C# code for the above approach
using System;
class GFG
{
// Function to check whether all elements
// can be converted to zero
static string Solve(int[] A, int N)
{
// Iterating for each bit position
for (int i = 0; i < 31; i++)
{
// Variable to store the number of
// set bits at a particular
// position
int setBitsCount = 0;
for (int j = 0; j < N; j++)
{
// Checking if ith bit is
// set or not
if ((A[j] & (1 << i)) != 0)
{
// Incrementing the count
setBitsCount += 1;
}
}
// Checking if the number of set
// bits is odd
if ((setBitsCount & 1) != 0)
{
return "NO";
}
}
return "YES";
}
static void Main()
{
int N = 4;
int[] A = { 2, 3, 4, 5 };
// Function call
Console.WriteLine(Solve(A, N));
}
}
// This code is contributed by Susobhan Akhuli
<script>
// JavaScript code for the above approach
// Function to check whether all elements can be converted to zero
function solve(A, N) {
// Iterating for each bit position
for (let i = 0; i < 31; i++) {
// Variable to store the number of set bits at a particular position
let setBitsCount = 0;
for (let j = 0; j < N; j++) {
// Checking if the ith bit is set or not
if (A[j] & (1 << i)) {
// Incrementing the count
setBitsCount += 1;
}
}
// Checking if the number of set bits is odd
if (setBitsCount & 1) return "NO";
}
return "YES";
}
let N = 4;
let A = [2, 3, 4, 5];
// Function call
document.write(solve(A, N));
// This code is contributed by Susobhan Akhuli
</script>
Output
YES
Time complexity: O(N * 31)
Auxiliary Space: O(1)