Skip to content
Related Articles

Related Articles

Check whether an Array can be made 0 by splitting and merging repeatedly
  • Difficulty Level : Medium
  • Last Updated : 16 Jun, 2020

Given an array arr[] with N elements, the task is to find whether all the elements of the given array can be made 0 by given operations. Only 2 types of operations can be performed on this array: 

  • Split an element B into 2 elements C and D such that B = C + D.
  • Merge 2 elements P and Q as one element R such that R = P^Q i.e. (XOR of P and Q).

Examples: 

Input:  arr = [9, 17]
Output: Yes
Explanation: Following is one possible sequence of operations –  
1) Merge i.e 9 XOR 17 = 24   
2) Split 24 into two parts each of size 12  
3) Merge i.e 12 XOR 12 = 0   
As there is only 1 element i.e 0. So it is possible.

Input:  arr = [1]
Output: No
Explanation: There is no possible way to make it 0.

Approach :



  1. If any element in the array is even then it can be made 0. Split that element in two equal parts of arr[i]/2 and arr[i]/2. XOR of two equal numbers is zero. Therefore this strategy makes an element 0.
  2. If any element is odd. Split it in two parts: 1 and arr[i]-1. Since arr[i]-1 is even, it can be made 0 by the above strategy. Therefore an odd element can reduce its size to 1. Two odd elements can, therefore, be made 0 by following above strategy and finally XOR them (i.e. 1) as 1 XOR 1 = 0. Therefore if the number of odd elements in the array is even, then the answer is possible.Otherwise, an element of value 1 will be left and it is not possible to satisfy the condition.

Below is the implementation of the above approach: 

C++




// C++ program for the above approach 
  
#include <bits/stdc++.h>
using namespace std;
  
// Function that finds if it is
// possible to make the array
// contain only 1 element i.e. 0
string solve(vector<int>& A)
{
    int i, ctr = 0;
    for (i = 0; i < A.size();
         i++) {
  
        // Check if element is odd
        if (A[i] % 2) {
            ctr++;
        }
    }
  
    // According to the logic
    // in above approach
    if (ctr % 2) {
        return "No";
    }
    else {
        return "Yes";
    }
}
  
// Driver code
int main()
{
  
    vector<int> arr = { 9, 17 };
  
    cout << solve(arr) << endl;
    return 0;
}

Java




// Java program for the above approach 
class GFG{
      
// Function that finds if it is 
// possible to make the array 
// contain only 1 element i.e. 0 
public static String solve(int[] A) 
{
    int i, ctr = 0;
          
    for(i = 0; i < A.length; i++)
    
      
       // Check if element is odd 
       if (A[i] % 2 == 1)
       
           ctr++; 
       
    
      
    // According to the logic 
    // in above approach 
    if (ctr % 2 == 1)
    
        return "No"
    
    else
    
        return "Yes"
    
}
  
// Driver code    
public static void main(String[] args)
{
    int[] arr = { 9, 17 };
    System.out.println(solve(arr));
}
}
  
// This code is contributed by divyeshrabadiya07

Python3




# Python3 program for the above approach 
  
# Function that finds if it is 
# possible to make the array 
# contain only 1 element i.e. 0 
def solve(A):
      
    ctr = 0
      
    for i in range(len(A)):
          
        # Check if element is odd 
        if A[i] % 2 == 1:
            ctr += 1
              
    # According to the logic 
    # in above approach 
    if ctr % 2 == 1:
        return 'No'
    else :
        return 'Yes'
      
# Driver code 
if __name__=='__main__':
      
    arr = [9, 17]
  
    print(solve(arr))
      
# This code is contributed by rutvik_56

C#




// C# program for the above approach 
using System;
  
class GFG{
      
// Function that finds if it is 
// possible to make the array 
// contain only 1 element i.e. 0 
public static string solve(int[] A) 
{
    int i, ctr = 0;
          
    for(i = 0; i < A.Length; i++)
    
          
       // Check if element is odd 
       if (A[i] % 2 == 1)
       
           ctr++; 
       
    
      
    // According to the logic 
    // in above approach 
    if (ctr % 2 == 1)
    
        return "No"
    
    else
    
        return "Yes"
    
}
  
// Driver code 
public static void Main()
{
    int[] arr = { 9, 17 };
      
    Console.Write(solve(arr));
}
}
  
// This code is contributed by chitranayal
Output:
Yes


Time Complexity: O(N)
Auxiliary Space Complexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :