# 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

Let’s denote XOR between all elements in the first group as G_{1} and XOR between all elements in the second group as G_{2}. Now, the following relation is always correct: G_{1} ⊕ G_{2} = A_{1} ⊕ A_{2} ⊕ …. ⊕ A_{n}.

So for G_{1} = G_{2}, xor between all elements of array A is equal to 0. So, in that case, answer will be (2^{n} – 2)/2 = (2^{n-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<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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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". ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` `?> ` |

*chevron_right*

*filter_none*

Output:

3

## Recommended Posts:

- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Number of ways to split a binary number such that every part is divisible by 2
- Reverse an array in groups of given size
- Minimum sum obtained from groups of four elements from the given array
- Reverse an array in groups of given size | Set 2 (Variations of Set 1 )
- Find array using different XORs of elements in groups of size 4
- Split the array and add the first part to the end
- Split the array and add the first part to the end | Set 2
- Split array in three equal sum subarrays
- Split an array into two equal Sum subarrays
- Split the array into equal sum parts according to given conditions
- Minimum value to add to arr[i] so that an array can be split at index i with equal sum
- Split the array into odd number of segments of odd lengths
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Minimum positive integer required to split the array equally

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.