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++
#include <bits/stdc++.h>
using namespace std;
string solve(vector< int >& A)
{
int i, ctr = 0;
for (i = 0; i < A.size();
i++) {
if (A[i] % 2) {
ctr++;
}
}
if (ctr % 2) {
return "No" ;
}
else {
return "Yes" ;
}
}
int main()
{
vector< int > arr = { 9, 17 };
cout << solve(arr) << endl;
return 0;
}
|
Java
class GFG{
public static String solve( int [] A)
{
int i, ctr = 0 ;
for (i = 0 ; i < A.length; i++)
{
if (A[i] % 2 == 1 )
{
ctr++;
}
}
if (ctr % 2 == 1 )
{
return "No" ;
}
else
{
return "Yes" ;
}
}
public static void main(String[] args)
{
int [] arr = { 9 , 17 };
System.out.println(solve(arr));
}
}
|
Python3
def solve(A):
ctr = 0
for i in range ( len (A)):
if A[i] % 2 = = 1 :
ctr + = 1
if ctr % 2 = = 1 :
return 'No'
else :
return 'Yes'
if __name__ = = '__main__' :
arr = [ 9 , 17 ]
print (solve(arr))
|
C#
using System;
class GFG{
public static string solve( int [] A)
{
int i, ctr = 0;
for (i = 0; i < A.Length; i++)
{
if (A[i] % 2 == 1)
{
ctr++;
}
}
if (ctr % 2 == 1)
{
return "No" ;
}
else
{
return "Yes" ;
}
}
public static void Main()
{
int [] arr = { 9, 17 };
Console.Write(solve(arr));
}
}
|
Javascript
<script>
function solve(A)
{
let i, ctr = 0;
for (i = 0; i < A.length;
i++) {
if (A[i] % 2) {
ctr++;
}
}
if (ctr % 2) {
return "No" ;
}
else {
return "Yes" ;
}
}
let arr = [ 9, 17 ];
document.write(solve(arr));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int A[] = { 9, 17 };
int n = sizeof (A) / sizeof (A[0]);
int xor1 = 0;
for ( int i = 0; i < n; i++) {
xor1 ^= A[i];
}
if (xor1 % 2 == 0) {
cout << "Yes\n" ;
}
else {
cout << "No\n" ;
}
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int [] A = { 9 , 17 };
int n = A.length;
int xor = 0 ;
for ( int i = 0 ; i < n; i++) {
xor ^= A[i];
}
if (xor % 2 == 0 ) {
System.out.print( "Yes" );
}
else {
System.out.print( "No" );
}
}
}
|
Python3
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"
if __name__ = = '__main__' :
arr = [ 9 , 17 ]
print (solve(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static void Main( string [] args)
{
int [] A = { 9, 17 };
int n = A.Length;
int xor = 0;
for ( int i = 0; i < n; i++) {
xor ^= A[i];
}
if (xor % 2 == 0) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
let A = [ 9, 17 ];
let n = A.length;
let xor = 0;
for ( var i = 0; i < n; i++) {
xor ^= A[i];
}
if (xor % 2 == 0) {
console.log( "Yes" );
}
else {
console.log( "No" );
}
|
Time Complexity: O(N)
Auxiliary Space Complexity: O(1)
Last Updated :
23 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...