Open In App

Ways to split array into two groups of same XOR value

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array A of n integers. The task is to count the number of ways to split given array elements into two disjoint groups, such that XOR of elements of each group is equal.

Examples: 

Input : A[] = { 1, 2, 3 }
Output : 3
{(1), (2, 3)}, {(2), (1, 3)}, {(3), (1, 2)} are three ways with equal XOR value of two groups.

Input :  A[] = { 5, 2, 3, 2 }
Output : 0

Recommended Practice

Let’s denote XOR between all elements in the first group as G1 and XOR between all elements in the second group as G2. Now, the following relation is always correct: G1 ⊕ G2 = A1 ⊕ A2 ⊕ …. ⊕ An
So for G1 = G2, xor between all elements of array A is equal to 0. So, in that case, answer will be (2n – 2)/2 = (2n-1 – 1). In second case, when XOR between all elements isn’t 0, we can not split array. Answer will be 0.

Implementation:

C++




// CPP Program to count number of ways to split
// array into two groups such that each group
// has equal XOR value
#include<bits/stdc++.h>
using namespace std;
 
// Return the count number of ways to split
// array into two  groups such that each group
// has equal XOR value.
int countgroup(int a[], int n)
{
  int xs = 0;
  for (int i = 0; i < n; i++)
    xs = xs ^ a[i];
 
  // We can split only if XOR is 0. Since
  // XOR of all is 0, we can consider all
  // subsets as one group.
  if (xs == 0)
    return (1 << (n-1)) - 1;
 
  return 0;
}
 
// Driver Program
int main()
{
  int a[] = { 1, 2, 3 };
  int n = sizeof(a)/sizeof(a[0]);
  cout << countgroup(a, n) << endl;
  return 0;
}


Java




// Java Program to count number of ways
// to split array into two groups such
// that each group has equal XOR value
import java.io.*;
import java.util.*;
 
class GFG {
 
// Return the count number of ways to split
// array into two groups such that each group
// has equal XOR value.
static int countgroup(int a[], int n) {
    int xs = 0;
    for (int i = 0; i < n; i++)
    xs = xs ^ a[i];
 
    // We can split only if XOR is 0. Since
    // XOR of all is 0, we can consider all
    // subsets as one group.
    if (xs == 0)
    return (1 << (n - 1)) - 1;
 
    return 0;
}
 
// Driver program
public static void main(String args[]) {
    int a[] = {1, 2, 3};
    int n = a.length;
    System.out.println(countgroup(a, n));
}
}
 
// This code is contributed by Nikita Tiwari.


Python3




# Python3 code to count number of ways
# to split array into two groups such
# that each group has equal XOR value
 
# Return the count of number of ways
# to split array into two groups such
# that each group has equal XOR value.
def countgroup(a, n):
    xs = 0
    for i in range(n):
        xs = xs ^ a[i]
     
    # We can split only if XOR is 0.
    # Since XOR of all is 0, we can
    # consider all subsets as one group.
    if xs == 0:
        return (1 << (n-1)) - 1
     
    return 0
     
# Driver Program
a = [1, 2, 3]
n = len(a)
print(countgroup(a, n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#




// C# Program to count number of ways
// to split array into two groups such
// that each group has equal XOR value
using System;
 
class GFG {
 
    // Return the count number of ways to split
    // array into two groups such that each group
    // has equal XOR value.
    static int countgroup(int[] a, int n)
    {
        int xs = 0;
        for (int i = 0; i < n; i++)
            xs = xs ^ a[i];
 
        // We can split only if XOR is 0. Since
        // XOR of all is 0, we can consider all
        // subsets as one group.
        if (xs == 0)
            return (1 << (n - 1)) - 1;
 
        return 0;
    }
 
    // Driver program
    public static void Main()
    {
        int[] a = { 1, 2, 3 };
        int n = a.Length;
        Console.WriteLine(countgroup(a, n));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP Program to count number
// of ways to split array into
// two groups such that each 
// group has equal XOR value
 
// Return the count number of
// ways to split array into
// two groups such that each 
// grouphas equal XOR value.
function countgroup($a, $n)
{
    $xs = 0;
    for ($i = 0; $i < $n; $i++)
        $xs = $xs ^ $a[$i];
     
    // We can split only if XOR is 0. Since
    // XOR of all is 0, we can consider all
    // subsets as one group.
    if ($xs == 0)
        return (1 << ($n - 1)) - 1;
     
    return 0;
}
 
// Driver Code
$a = array(1, 2, 3);
$n = count($a);
echo countgroup($a, $n);
 
// This code is contributed by anuj_67.
?>


Javascript




<script>
 
      // JavaScript Program to count number of ways to split
      // array into two groups such that each group
      // has equal XOR value
 
      // Return the count number of ways to split
      // array into two groups such that each group
      // has equal XOR value.
      function countgroup(a, n) {
        var xs = 0;
        for (var i = 0; i < n; i++) xs = xs ^ a[i];
 
        // We can split only if XOR is 0. Since
        // XOR of all is 0, we can consider all
        // subsets as one group.
        if (xs == 0) return (1 << (n - 1)) - 1;
      }
 
      // Driver Program
       
      var a = [1, 2, 3];
      var n = a.length;
      document.write(countgroup(a, n) + "<br>");
       
</script>


Output

3

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



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