Ways to split array into two groups of same XOR value - GeeksforGeeks

14 Apr, 2021
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
 
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.  
C++
Java
Python3
C#
PHP
Javascript
// 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 Programint main(){  int a[] = { 1, 2, 3 };  int n = sizeof(a)/sizeof(a[0]);  cout << countgroup(a, n) << endl;  return 0;}
// Java Program to count number of ways// to split array into two groups such// that each group has equal XOR valueimport 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 programpublic 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 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 Programa = [1, 2, 3]n = len(a)print(countgroup(a, n)) # This code is contributed by "Sharad_Bhardwaj".
// C# Program to count number of ways// to split array into two groups such// that each group has equal XOR valueusing 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 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.?>
<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
 
vt_m
rdtank
Bitwise-XOR
Arrays
Bit Magic
Arrays
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Window Sliding Technique
Trapping Rain Water
Building Heap from Array
Program to find sum of elements in a given array
Reversal algorithm for array rotation
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Cyclic Redundancy Check and Modulo-2 Division
Count set bits in an integer