Open In App

Check if Binary Array can be split into X Subarray with same bitwise XOR

Last Updated : 10 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to implement the approach
#include <iostream>
#include <vector>
using namespace std;
 
// Function to find check whether
// array can be divided into exactly
// X non-empty subarrays
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";
}
 
// Driver Code
int main() {
    vector<int> A = { 0, 1, 1, 0, 0 };
    int N = A.size();
    int X = 3;
     
    // Function Call
    cout << check(A, N, X) << endl;
    return 0;
}
 
// This code is contributed Tapesh(tapeshdua420)


Java




// Java code to implement the approach
 
import java.io.*;
import java.util.*;
 
public class GFG {
 
    // Function to find check whether
    // array can be divided into exactly
    // X non-empty subarrays
    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";
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] A = { 0, 1, 1, 0, 0 };
        int N = A.length;
        int X = 3;
 
        // Function Call
        System.out.println(check(A, N, X));
    }
}


Python3




# Python code to implement the approach
 
# Function to find check whether
# array can be divided into exactly
# X non-empty subarrays
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"
 
# Driver Code
if __name__ == '__main__':
    A = [0, 1, 1, 0, 0
    N = len(A) 
    X = 3
     
    # Function Call
    print(check(A, N, X))
     
# This code is contributed Tapesh(tapeshdua420)


C#




// C# code to implement the approach
using System;
 
class Program {
    // Driver Code
    static void Main(string[] args)
    {
        int[] A = { 0, 1, 1, 0, 0 };
        int N = A.Length;
        int X = 3;
 
        // Function Call
        Console.WriteLine(check(A, N, X));
    }
 
    // Function to find check whether
    // array can be divided into exactly
    // X non-empty subarrays
    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";
    }
}
 
// This code is contributed by Tapesh(tapeshdua420)


Javascript




// JavaScript code to implement the approach
 
// Function to find check whether
// array can be divided into exactly
// X non-empty subarrays
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"
}
 
// Driver Code
var A = [ 0, 1, 1, 0, 0 ]
var N = A.length
var X = 3
 
// Function Call
console.log(check(A, N, X))
 
// This code is contributed Tapesh(tapeshdua420).


Output

Yes

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads