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

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 :Â
Â
- 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.
- 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. 0string 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 codeint 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. 0function 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> |
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 codeif __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 approachlet A = [ 9, 17 ];Â
// length of the arraylet n = A.length;Â
// variable to store the value of XORlet xor = 0;Â
// traversing the arrayfor (var i = 0; i < n; i++) {Â Â Â Â xor ^= A[i];}Â
// checking if the value of XOR is even or odd// if even printing YES else ONOif (xor % 2 == 0) {Â Â Â Â console.log("Yes");}else {Â Â Â Â console.log("No");}Â
// This code is contributed by phasing17 |
Yes
Time Complexity: O(N)Â
Auxiliary Space Complexity: O(1)Â
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



