Make all elements of the Array zero
Last Updated :
17 Aug, 2023
Given, an array A of length N, the task is to make every element of the array equal to zero, by doing some number of moves (possibly zero). In a single move, you can choose two integers l and r such that
1 ≤ l ≤ r ≤ N. Let x = A[l] ^ A[l+1] ^ A[l+2] … A[r], where ^ represents xor operator. Then, replace the whole subarray with x. In other words, assign A[i] = x for l ≤ i ≤ r.
Examples:
Input: N = 4, A[] = {7, 1, 2, 0}
Output: 2
Explanation: 1st operation: select subarray from 1 to 3 (1-indexed) x = 4 (7 ^ 1 ^ 2). So the array now becomes [4, 0].
2nd operation: select subarray from 1 to 1 (1-indexed) x = 0 (4 ^ 4). So the array becomes [0, 0].
Input: N = 2, A[] = {2, 2}
Output: 1
Explanation: 1st operation: select the entire array, the resultant xor will be 0. So the array.
Approach: To solve the problem follow the below idea:
This problem is observation-based. If all the elements of the array are equal to zero then it’s evident that the answer is 0. If the xor of the entire array is zero, then the number of operations required is 1, to xor the entire array. If the above two conditions are not met then the answer will be two because in one operation we xor the entire array which will be ending up getting some value greater than 0. For the second operation, since now we have only one positive integer we can xor the same number by itself, which will make the value as zero.
Steps that were to follow the above approach:
- Initialize two variables xor and isZero with zero and true respectively. Xor will store xor of the entire array and isZero will store true if the entire array is zero, or else it will store false.
- Iterate over the array and find out the xor and if any element is greater than zero, update the variables accordingly.
- If isZero is true print 0 or if the xor for the entire array is zero print 1 else print 2.
Below is the code to implement the above steps:
C++
#include <bits/stdc++.h>
using namespace std;
int makeZero(vector< int >& A, int n)
{
int Xor = 0;
bool isZero = true ;
for ( int i = 0; i < n; i++) {
if (A[i] > 0)
isZero = false ;
Xor = Xor ^ A[i];
}
if (isZero == true )
return 0;
else if (Xor == 0)
return 1;
return 2;
}
int main()
{
int n = 4;
vector< int > A = { 7, 1, 2, 0 };
cout << makeZero(A, n);
return 0;
}
|
Java
public class GFG
{
public static int makeZero( int [] A, int n)
{
int Xor= 0 ;
boolean isZero= true ;
for ( int i= 0 ;i<n;i++)
{
if (A[i] > 0 )
isZero= false ;
Xor = Xor ^ A[i];
}
if (isZero == true )
return 0 ;
else if (Xor == 0 )
return 1 ;
return 2 ;
}
public static void main(String[] args) {
int n= 4 ;
int A[]={ 7 , 1 , 2 , 0 };
System.out.println(makeZero(A,n));
}
}
|
Python3
def makeZero(l,n):
xor = 0
isZero = True
for i in range (n):
if l[i]> 0 :
isZero = False
xor = xor ^ l[i];
if isZero is True :
return 0
elif xor = = 0 :
return 1
return 2
n = 4
l = [ 7 , 1 , 2 , 0 ]
print (makeZero(l,n))
|
C#
using System;
public class GFG {
public int makeZero( int [] A, int n)
{
int Xor=0;
bool isZero= true ;
for ( int i=0;i<n;i++)
{
if (A[i] > 0)
isZero= false ;
Xor = Xor ^ A[i];
}
if (isZero == true )
return 0;
else if (Xor == 0)
return 1;
return 2;
}
static void Main() {
int n=4;
int [] A = { 7, 1, 2, 0 };
GFG gfg = new GFG();
Console.WriteLine(gfg.makeZero(A,n));
}
}
|
Javascript
function makeZero(A, n) {
let Xor = 0;
let isZero = true ;
for (let i = 0; i < n; i++) {
if (A[i] > 0)
isZero = false ;
Xor = Xor ^ A[i];
}
if (isZero === true )
return 0;
else if (Xor === 0)
return 1;
return 2;
}
const n = 4;
const A = [7, 1, 2, 0];
console.log(makeZero(A, n));
|
Time Complexity: O(N), Where N is the length of the array.
Auxiliary Space: O(1), Since we are not using extra space.
Share your thoughts in the comments
Please Login to comment...