Check if Binary Array can be split into X Subarray with same bitwise XOR
Last Updated :
10 Nov, 2022
Given a binary array A[] and an integer X, the task is to check whether it is possible to divide A[] into exactly X non-empty and non-overlapping subarrays such that each Ai belongs to exactly one subarray and the bitwise XOR of each subarray is the same.
Examples:
Input: A[] = {0, 1, 1, 0, 0}, X = 3
Output: Yes
?Explanation: One of the possible ways of dividing A is {0}, {1, 1} & {0, 0}. Here XOR of each subarray is 0.
Input: A[] = {1, 1, 1}, X = 2
?Output: No
Approach: The problem can be solved based on the following observation:
The bitwise XOR of any binary array is either 0 or 1. Therefore if an answer exists then it is either X non-overlapping subarrays having XOR equal to 1 or X non – overlapping subarrays having XOR equal to 0. We can iterate over the binary array and check whether we can divide the array into X non-overlapping subarrays having XOR equal to 0 or X non-overlapping substrings having XOR equal to 1.
Follow the steps mentioned below to implement the above idea:
- First set xor = 0, count0 = 0 and count1 = 0 .
- Iterate a loop to count the number of times the xor of the prefix element of the array is 0. Let’s say the count is count0.
- After that check count0 ? X and xor != 1, if it is true then return “Yes”.
- If it is not true set the xor = 0.
- Iterate another loop to count the number of times the xor of the prefix element of the array is 1 and reset xor = 0. Let’s say the count is count1.
- After that check count1 ? X and (count1 – X) % 2 == 0, if it is true then return “Yes”.
- Otherwise, return “No”.
Below is the implementation of the above approach.
C++
#include <iostream>
#include <vector>
using namespace std;
string check(vector< int > &arr, int n, int x)
{
int xor_ = 0;
int count0 = 0, count1 = 0;
for ( int i = 0; i < n; i++) {
xor_ ^= arr[i];
if (xor_ == 0)
count0++;
}
if (count0 >= x && xor_ != 1) {
return "Yes" ;
}
xor_ = 0;
for ( int i = 0; i < n; i++) {
xor_ ^= arr[i];
if (xor_ == 1) {
count1++;
xor_ = 0;
}
}
if (count1 >= x && (count1 - x) % 2 == 0) {
return "Yes" ;
}
return "No" ;
}
int main() {
vector< int > A = { 0, 1, 1, 0, 0 };
int N = A.size();
int X = 3;
cout << check(A, N, X) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static String check( int arr[], int n, int x)
{
int xor = 0 ;
int count0 = 0 , count1 = 0 ;
for ( int i = 0 ; i < n; i++) {
xor ^= arr[i];
if (xor == 0 )
count0++;
}
if (count0 >= x && xor != 1 ) {
return "Yes" ;
}
xor = 0 ;
for ( int i = 0 ; i < n; i++) {
xor ^= arr[i];
if (xor == 1 ) {
count1++;
xor = 0 ;
}
}
if (count1 >= x && (count1 - x) % 2 == 0 ) {
return "Yes" ;
}
return "No" ;
}
public static void main(String[] args)
{
int [] A = { 0 , 1 , 1 , 0 , 0 };
int N = A.length;
int X = 3 ;
System.out.println(check(A, N, X));
}
}
|
Python3
def check(arr, n, x):
xor = 0
count0 = 0
count1 = 0
for i in range (n):
xor ^ = arr[i]
if xor = = 0 :
count0 + = 1
if count0 > = x and xor ! = 1 :
return "Yes"
xor = 0
for i in range (n):
xor ^ = arr[i]
if xor = = 1 :
count1 + = 1
xor = 0
if count1 > = x and (count1 - x) % 2 = = 0 :
return "Yes"
return "No"
if __name__ = = '__main__' :
A = [ 0 , 1 , 1 , 0 , 0 ]
N = len (A)
X = 3
print (check(A, N, X))
|
C#
using System;
class Program {
static void Main( string [] args)
{
int [] A = { 0, 1, 1, 0, 0 };
int N = A.Length;
int X = 3;
Console.WriteLine(check(A, N, X));
}
public static string check( int [] arr, int n, int x)
{
int xor = 0;
int count0 = 0, count1 = 0;
for ( int i = 0; i < n; i++) {
xor ^= arr[i];
if (xor == 0)
count0++;
}
if (count0 >= x && xor != 1) {
return "Yes" ;
}
xor = 0;
for ( int i = 0; i < n; i++) {
xor ^= arr[i];
if (xor == 1) {
count1++;
xor = 0;
}
}
if (count1 >= x && ((count1 - x) % 2 == 0)) {
return "Yes" ;
}
return "No" ;
}
}
|
Javascript
function check(arr, n, x) {
let xor = 0
let count0 = 0, count1 = 0
for (let i = 0; i < n; i++) {
xor ^= arr[i]
if (xor == 0)
count0++
}
if (count0 >= x && xor != 1) {
return "Yes"
}
xor = 0
for (let i = 0; i < n; i++) {
xor ^= arr[i]
if (xor == 1) {
count1++
xor = 0
}
}
if (count1 >= x && (count1 - x) % 2 == 0) {
return "Yes"
}
return "No"
}
var A = [ 0, 1, 1, 0, 0 ]
var N = A.length
var X = 3
console.log(check(A, N, X))
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...