Open In App

Make all elements of the Array zero

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:

Below is the code to implement the above steps:




// C++ code for the above approach.
#include <bits/stdc++.h>
using namespace std;
 
// Function to make array elements 0
int makeZero(vector<int>& A, int n)
{
 
    // Initialising Xor with 0 and isZero
    // flag as true. Xor will store xor
    // of entire array. isZero flag store
    // true if entire array is zero and
    // flase if even one element is > 0.
    int Xor = 0;
    bool isZero = true;
 
    // Computing xor for the entire array.
    // Checking if array contains all
    // zeroes or not.
    for (int i = 0; i < n; i++) {
        if (A[i] > 0)
            isZero = false;
        Xor = Xor ^ A[i];
    }
 
    // Returning the result.
    if (isZero == true)
        return 0;
    else if (Xor == 0)
        return 1;
    return 2;
}
 
// Driver's code
int main()
{
    int n = 4;
    vector<int> A = { 7, 1, 2, 0 };
 
    // Function call
    cout << makeZero(A, n);
 
    return 0;
}




// Java code for the above approach.
public class GFG
{
    // Function to make array elements 0
    public static int makeZero(int[] A,int n)
    {
         
        // Initialising Xor with 0 and isZero
        // flag as true. Xor will store xor
        // of entire array. isZero flag store
        // true if entire array is zero and
        // flase if even one element is > 0.
        int Xor=0;
        boolean isZero=true;
       
       
        // Computing xor for the entire array.
        // Checking if array contains all
        // zeroes or not.
          for(int i=0;i<n;i++)
          {
              if(A[i] > 0)
              isZero=false;
               
            Xor = Xor ^ A[i];
          }
           
           
        // Returning the result.
        if (isZero == true)
            return 0;
        else if (Xor == 0)
            return 1;
        return 2;
    }
     
     
    // Driver's code
    public static void main(String[] args) {
         
        int n=4;
        int A[]={ 7, 1 , 2, 0};
         
        System.out.println(makeZero(A,n));
    }
}




# Python code for the above approach.
 
 
# Function to make array elements 0
def makeZero(l,n):
     
     
    # Initialising Xor with 0 and isZero
    # flag as true. Xor will store xor
    # of entire array. isZero flag store
    # true if entire array is zero and
    # flase if even one element is > 0.
    xor=0
    isZero=True
     
     
    # Computing xor for the entire array.
    # Checking if array contains all
    # zeroes or not.
    for i in range(n):
        if l[i]>0:
            isZero=False
        xor = xor ^ l[i];
         
    # Returning the result.  
    if isZero is True:
        return 0
    elif xor == 0:
        return 1
    return 2
 
 
# Driver's code
n=4
l=[7,1,2,0]
 
print(makeZero(l,n))




// C# code for the above approach.
using System;
 
public class GFG {
     
  // Function to make array elements 0
  public int makeZero(int[] A,int n)
  {
       
    // Initialising Xor with 0 and isZero
    // flag as true. Xor will store xor
    // of entire array. isZero flag store
    // true if entire array is zero and
    // flase if even one element is > 0.
      int Xor=0;
      bool isZero=true;
       
       
    // Computing xor for the entire array.
    // Checking if array contains all
    // zeroes or not.
      for(int i=0;i<n;i++)
      {
          if(A[i] > 0)
          isZero=false;
           
        Xor = Xor ^ A[i];
      }
       
       
    // Returning the result.
    if (isZero == true)
        return 0;
    else if (Xor == 0)
        return 1;
    return 2;
  }
    
  // Driver's code
  static void Main() {
       
    int n=4;
    int[] A = { 7, 1, 2, 0 };
     
    GFG gfg = new GFG();
     
    Console.WriteLine(gfg.makeZero(A,n));
     
  }
   
}




// Function to make array elements 0
function makeZero(A, n) {
 
  // Initialising Xor with 0 and isZero
  // flag as true. Xor will store xor
  // of entire array. isZero flag store
  // true if entire array is zero and
  // flase if even one element is > 0.
  let Xor = 0;
  let isZero = true;
 
    // Computing xor for the entire array.
    // Checking if array contains all
    // zeroes or not.
  for (let i = 0; i < n; i++) {
    if (A[i] > 0)
      isZero = false;
    Xor = Xor ^ A[i];
  }
 
    // Returning the result.
  if (isZero === true)
    return 0;
  else if (Xor === 0)
    return 1;
  return 2;
}
 
// Driver code
const n = 4;
const A = [7, 1, 2, 0];
 
console.log(makeZero(A, n));

Output
2

Time Complexity: O(N), Where N is the length of the array.
Auxiliary Space: O(1), Since we are not using extra space.


Article Tags :