Open In App

Check whether an Array can be made 0 by splitting and merging repeatedly

Improve
Improve
Like Article
Like
Save
Share
Report

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).

You have to determine whether it is possible to convert array A to size 1, containing a single element equal to 0 after several splits and/or merges?

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 into 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 the 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


Javascript




<script>
 
// Javascript program for the above approach
 
// Function that finds if it is
// possible to make the array
// contain only 1 element i.e. 0
function solve(A)
{
    let i, ctr = 0;
    for (i = 0; i < A.length;
         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
 
    let arr = [ 9, 17 ];
 
    document.write(solve(arr));
 
</script>


Output

Yes

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

Another Approach:

The approach is rather simple, we just have to find the XOR of the elements of the array and if it’s odd, then dividing or splitting it will be of any use as every time the value of XOR will always come odd, and if it’s even we have our answer i.e. 0.

C++




// C++ program to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
  int A[] = { 9, 17 };
 
  // length of the array
  int n = sizeof(A) / sizeof(A[0]);
 
  // variable to store the value of XOR
  int xor1 = 0;
 
  // traversing the array
  for (int i = 0; i < n; i++) {
    xor1 ^= A[i];
  }
 
  // checking if the value of XOR is even or odd
  // if even printing YES else ONO
  if (xor1 % 2 == 0) {
    cout << "Yes\n";
  }
  else {
    cout << "No\n";
  }
}
 
// This code is contributed by phasing17


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int[] A = { 9, 17 };
        // length of the array
        int n = A.length;
        // variable to store the value of XOR
        int xor = 0;
        // traversing the array
        for (int i = 0; i < n; i++) {
            xor ^= A[i];
        }
        // checking if the value of XOR is even or odd
        // if even printing YES else ONO
        if (xor % 2 == 0) {
            System.out.print("Yes");
        }
        else {
            System.out.print("No");
        }
    }
}


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):
    n = len(A)
    xor = 0
    for i in range(n):
        xor ^= A[i]
    if(xor % 2 == 0):
        return "YES"
    else:
        return "NO"
 
 
# Driver code
if __name__ == '__main__':
 
    arr = [9, 17]
    print(solve(arr))


C#




// C# program to implement the approach
 
using System;
using System.Collections.Generic;
 
class GFG {
    public static void Main(string[] args)
    {
        int[] A = { 9, 17 };
        // length of the array
        int n = A.Length;
        // variable to store the value of XOR
        int xor = 0;
        // traversing the array
        for (int i = 0; i < n; i++) {
            xor ^= A[i];
        }
        // checking if the value of XOR is even or odd
        // if even printing YES else ONO
        if (xor % 2 == 0) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }
}
 
 
// This code is contributed by phasing17


Javascript




// JS program to implement the approach
let A = [ 9, 17 ];
 
// length of the array
let n = A.length;
 
// variable to store the value of XOR
let xor = 0;
 
// traversing the array
for (var i = 0; i < n; i++) {
    xor ^= A[i];
}
 
// checking if the value of XOR is even or odd
// if even printing YES else ONO
if (xor % 2 == 0) {
    console.log("Yes");
}
else {
    console.log("No");
}
 
// This code is contributed by phasing17


Output

Yes

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



Last Updated : 23 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads