给定n个数字的数组arr []和数K,找到元素XOR为K的arr []的子集数
例子 :
Input: arr[] = {6, 9, 4,2}, k = 6
Output: 2
The subsets are {4, 2} and {6}
Input: arr[] = {1, 2, 3, 4, 5}, k = 4
Output: 4
The subsets are {1, 5}, {4}, {1, 2, 3, 4}
and {2, 3, 5}
我们强烈建议您单击此处并进行实践,然后再继续解决方案。
蛮力法O(2 n ):一种幼稚的方法是生成所有2 n个子集,并对具有XOR值K的所有子集进行计数,但是这种方法对于n的较大值将无效。
动态规划方法O(n * m):
我们定义一个数字m,使m = pow(2,(log2(max(arr))+ 1))–1。该数字实际上是任何XOR子集都将获取的最大值。我们通过计算最大数字中的位数来获得此数字。我们创建一个2D数组dp [n + 1] [m + 1],使得dp [i] [j]等于arr [0…i-1]的子集中具有XOR值j的子集的数量。
我们按以下方式填充dp数组:
- 我们将dp [i] [j]的所有值初始化为0。
- 由于空集的XOR为0,因此dp [0] [0]的设置值= 1。
- 从左到右迭代arr [i]的所有值,并为每个arr [i]迭代XOR的所有可能值,即从0到m(包括两端),并如下填充dp数组:
对于i = 1到n:
对于j = 0到m:
dp [i] [j] = dp [i-1] [j] + dp [i-1] [j ^ arr [i-1]]
这可以解释为,如果存在一个XOR值为j的子集arr [0…i-2],那么也存在一个XOR值为j的子集arr [0…i-1]。同样,如果存在一个XOR值为j ^ arr [i]的子集arr [0….i-2],那么显然存在一个XOR值为j的子集arr [0…i-1],如j ^ arr [i- 1] ^ arr [i-1] = j。 - 计算具有XOR值k的子集的数量:由于dp [i] [j]是来自arr [0..i-1]的子集中具有j作为XOR值的子集的数量,因此来自集合arr的子集的数量XOR值为K的[0..n]将为dp [n] [K]
C++
// arr dynamic programming solution to finding the number
// of subsets having xor of their elements as k
#include
using namespace std;
// Returns count of subsets of arr[] with XOR value equals
// to k.
int subsetXOR(int arr[], int n, int k)
{
// Find maximum element in arr[]
int max_ele = arr[0];
for (int i=1; i max_ele)
max_ele = arr[i];
// Maximum possible XOR value
int m = (1 << (int)(log2(max_ele) + 1) ) - 1;
if( k > m )
return 0;
// The value of dp[i][j] is the number of subsets having
// XOR of their elements as j from the set arr[0...i-1]
int dp[n+1][m+1];
// Initializing all the values of dp[i][j] as 0
for (int i=0; i<=n; i++)
for (int j=0; j<=m; j++)
dp[i][j] = 0;
// The xor of empty subset is 0
dp[0][0] = 1;
// Fill the dp table
for (int i=1; i<=n; i++)
for (int j=0; j<=m; j++)
dp[i][j] = dp[i-1][j] + dp[i-1][j^arr[i-1]];
// The answer is the number of subset from set
// arr[0..n-1] having XOR of elements as k
return dp[n][k];
}
// Driver program to test above function
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int k = 4;
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Count of subsets is " << subsetXOR(arr, n, k);
return 0;
}
Java
// Java dynamic programming solution
// to finding the number of subsets
// having xor of their elements as k
class GFG{
// Returns count of subsets of arr[] with
// XOR value equals to k.
static int subsetXOR(int []arr, int n, int k)
{
// Find maximum element in arr[]
int max_ele = arr[0];
for(int i = 1; i < n; i++)
if (arr[i] > max_ele)
max_ele = arr[i];
// Maximum possible XOR value
int m = (1 << (int)(Math.log(max_ele) /
Math.log(2) + 1) ) - 1;
if (k > m)
{
return 0;
}
// The value of dp[i][j] is the number
// of subsets having XOR of their
// elements as j from the set arr[0...i-1]
int [][]dp = new int[n + 1][m + 1];
// Initializing all the values of dp[i][j] as 0
for(int i = 0; i <= n; i++)
for(int j = 0; j <= m; j++)
dp[i][j] = 0;
// The xor of empty subset is 0
dp[0][0] = 1;
// Fill the dp table
for(int i = 1; i <= n; i++)
for(int j = 0; j <= m; j++)
dp[i][j] = dp[i - 1][j] +
dp[i - 1][j ^ arr[i - 1]];
// The answer is the number of
// subset from set arr[0..n-1]
// having XOR of elements as k
return dp[n][k];
}
// Driver code
public static void main(String arg[])
{
int []arr = { 1, 2, 3, 4, 5 };
int k = 4;
int n = arr.length;
System.out.println("Count of subsets is " +
subsetXOR(arr, n, k));
}
}
// This code is contributed by rutvik_56
Python3
# Python 3 arr dynamic programming solution
# to finding the number of subsets having
# xor of their elements as k
import math
# Returns count of subsets of arr[] with
# XOR value equals to k.
def subsetXOR(arr, n, k):
# Find maximum element in arr[]
max_ele = arr[0]
for i in range(1, n):
if arr[i] > max_ele :
max_ele = arr[i]
# Maximum possible XOR value
m = (1 << (int)(math.log2(max_ele) + 1)) - 1
if( k > m ):
return 0
# The value of dp[i][j] is the number
# of subsets having XOR of their elements
# as j from the set arr[0...i-1]
# Initializing all the values
# of dp[i][j] as 0
dp = [[0 for i in range(m + 1)]
for i in range(n + 1)]
# The xor of empty subset is 0
dp[0][0] = 1
# Fill the dp table
for i in range(1, n + 1):
for j in range(m + 1):
dp[i][j] = (dp[i - 1][j] +
dp[i - 1][j ^ arr[i - 1]])
# The answer is the number of subset
# from set arr[0..n-1] having XOR of
# elements as k
return dp[n][k]
# Driver Code
arr = [1, 2, 3, 4, 5]
k = 4
n = len(arr)
print("Count of subsets is",
subsetXOR(arr, n, k))
# This code is contributed
# by sahishelangia
C#
// C# dynamic programming solution to finding the number
// of subsets having xor of their elements as k
using System;
class GFG
{
// Returns count of subsets of arr[] with
// XOR value equals to k.
static int subsetXOR(int []arr, int n, int k)
{
// Find maximum element in arr[]
int max_ele = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max_ele)
max_ele = arr[i];
// Maximum possible XOR value
int m = (1 << (int)(Math.Log(max_ele,2) + 1) ) - 1;
if( k > m ){
return 0;
}
// The value of dp[i][j] is the number of subsets having
// XOR of their elements as j from the set arr[0...i-1]
int [,]dp=new int[n+1,m+1];
// Initializing all the values of dp[i][j] as 0
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
dp[i, j] = 0;
// The xor of empty subset is 0
dp[0, 0] = 1;
// Fill the dp table
for (int i = 1; i <= n; i++)
for (int j = 0; j <= m; j++)
dp[i, j] = dp[i-1, j] + dp[i-1, j^arr[i-1]];
// The answer is the number of subset from set
// arr[0..n-1] having XOR of elements as k
return dp[n, k];
}
// Driver code
static public void Main ()
{
int []arr = {1, 2, 3, 4, 5};
int k = 4;
int n = arr.Length;
Console.WriteLine ("Count of subsets is " + subsetXOR(arr, n, k));
}
}
// This code is contributed by jit_t.
PHP
$max_ele)
$max_ele = $arr[$i];
// Maximum possible XOR value
$m = (1 << (int)(log($max_ele,
2) + 1) ) - 1;
if( $k > $m ){
return 0;
}
// The value of dp[i][j] is the
// number of subsets having
// XOR of their elements as j
// from the set arr[0...i-1]
// Initializing all the
// values of dp[i][j] as 0
for ($i = 0; $i <= $n; $i++)
for ($j = 0; $j <= $m; $j++)
$dp[$i][$j] = 0;
// The xor of empty subset is 0
$dp[0][0] = 1;
// Fill the dp table
for ($i = 1; $i <= $n; $i++)
for ( $j = 0; $j <= $m; $j++)
$dp[$i][$j] = $dp[$i - 1][$j] +
$dp[$i - 1][$j ^
$arr[$i - 1]];
// The answer is the number
// of subset from set arr[0..n-1]
// having XOR of elements as k
return $dp[$n][$k];
}
// Driver Code
$arr = array (1, 2, 3, 4, 5);
$k = 4;
$n = sizeof($arr);
echo "Count of subsets is " ,
subsetXOR($arr, $n, $k);
// This code is contributed by ajit
?>
Javascript
输出 :
Count of subsets is 4