Dada una array A de n enteros. La tarea es contar el número de formas de dividir los elementos de array dados en dos grupos separados, de modo que el XOR de los elementos de cada grupo sea igual.
Ejemplos:
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
Denotemos XOR entre todos los elementos del primer grupo como G 1 y XOR entre todos los elementos del segundo grupo como G 2 . Ahora, la siguiente relación siempre es correcta: G 1 ⊕ G 2 = A 1 ⊕ A 2 ⊕ …. ⊕ un n .
Entonces, para G 1 = G 2 , xor entre todos los elementos de la array A es igual a 0. Entonces, en ese caso, la respuesta será (2 n – 2)/2 = (2 n-1 – 1). En el segundo caso, cuando XOR entre todos los elementos no es 0, no podemos dividir la array. La respuesta será 0.
Implementación:
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>
Producción
3