📌  相关文章
📜  将数组分成具有相同XOR值的两组的方法

📅  最后修改于: 2021-05-25 00:48:07             🧑  作者: Mango

给定一个由n个整数组成的数组A。任务是计算将给定数组元素分为两个不相交的组的方法的数量,以使每个组的元素的XOR相等。
例子:

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

让我们将第一组中所有元素之间的XOR表示为G 1 ,将第二组中所有元素之间的XOR表示为G 2 。现在,下面的关系是总是正确的:克1-⊕2- = A 1⊕A 2 …⊕。 ⊕A n
因此,对于G 1 = G 2 ,数组A的所有元素之间的xor等于0。因此,在这种情况下,答案将是(2 n – 2)/ 2 =(2 n-1 – 1)。在第二种情况下,当所有元素之间的XOR不为0时,我们将无法拆分数组。答案将为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


Javascript


输出:

3