# Ways to split array into two groups of same XOR value

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: Please solve it on PRACTIEC first, before moving on to the solution.

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

 `// CPP Program to count number of ways to split  ` `// array into two groups such that each group  ` `// has equal XOR value  ` `#include ` `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

 ` `

Output:

```3
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.