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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Count of ways to split given string into two non-empty palindromes
- Count number of ways to divide an array into two halves with same sum
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Partition the array into two odd length groups with minimized absolute difference between their median
- Split a BST into two balanced BSTs based on a value K
- Find all possible ways to Split the given string into Primes
- Divide N segments into two non-empty groups such that given condition is satisfied
- Split an array into two equal Sum subarrays
- Split array into two subarrays such that difference of their maximum is minimum
- Split array into two subarrays such that difference of their sum is minimum
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Split array into two subsequences having minimum count of pairs with sum equal to X
- Find the size of largest group where groups are according to the xor of digits
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Divide binary array into three equal parts with same value
- Rearrange array to make Bitwise XOR of similar indexed elements of two arrays is same
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Split N into two integers whose addition to A and B makes them equal
- Split numbers from 1 to N into two equal sum subsets

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.