# Count number of subsets having a particular XOR value in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

Given an array arr[ ] containing positive integers and a value match. The goal is to find the subsets of arr[] that contain elements that have XOR = match.

For Example

## Input

arr[] = {4, 2, 8, 10} match=12

## Output

Count of number of subsets having a particular XOR value are: 2

## Explanation

Subsets of arr with XOR of elements as 0 are −
[ 4,8 ], [4,2,10]

## Input

arr[] = {3,5,2,7} match=5

## Output

Count of number of subsets having a particular XOR value are− 2

## Explanation

ubsets of arr with XOR of elements as 0 are−
[ 5 ], [2,7]

Approach used in the below program is as follows

In this approach we will use a dynamic programming solution. The array arr_2[][] will contain values at index i,j such that arr_2[ i ][ j ] has value XOR of elements from subsets of arr[ 0 to i−1 ]. Take the initial value arr_2[0][0] as 1, as for an empty set XOR is 1.Set arr_2[i][j] = arr_2[i−1][j] + arr_2[i−1][j ^ arr[i−1]], If subset arr[0 to i−2] has XOR as j then subset arr[0 to i−1] also has XOR j. Also if subset arr[0 to i−2] has XOR as j^arr[i] then subset arr[0 to i−1] also has XOR j as j ^ arr[i−1] ^ arr[i−1].The result will be found in arr_2[ size ][ match ].

• Take an integer array arr[].

• Take variable match as integer.

• Function subset_XOR(int arr[], int size, int match) takes an input array and its length and returns a count of the number of subsets having a particular XOR value.

• Initially take highest = arr[0]. Now traverse the whole arr[] using for loop and find the maximum value as highest.

• Compute temp = (1 << (int)(log2(highest) + 1) ) − 1 as the maximum possible XOR value.

• Take array arr_2[size+1][temp+1] to store XORs.

• Initialize whole arr_2 with 0s using for loop.

• Set arr_2[0][0] = 1.

• Using for loop from i=0 to i<=size, and j=0 to j<=size, set temp_2 = arr_2[i−1][j ^ arr[i−1]] and set arr_2[i][j] = arr_2[i−1][j] + temp_2.

• At the end of both for loops we will have arr_2[size][match] as count of number of subsets having a particular XOR value.

• Return arr_2[size][match] as result.

## Example

Live Demo

#include<bits/stdc++.h>
using namespace std;
int subset_XOR(int arr[], int size, int match){
int highest = arr[0];
for (int i = 1; i < size; i++){
if(arr[i] > highest){
highest = arr[i];
}
}
int temp = (1 << (int)(log2(highest) + 1) ) − 1;
if( match > temp){
return 0;
}
int arr_2[size+1][temp+1];
for (int i = 0; i<= size; i++){
for (int j = 0; j<= temp; j++){
arr_2[i][j] = 0;
}
}
arr_2[0][0] = 1;
for (int i=1; i<=size; i++){
for (int j=0; j<=temp; j++){
int temp_2 = arr_2[i−1][j ^ arr[i−1]];
arr_2[i][j] = arr_2[i−1][j] + temp_2;
}
}
return arr_2[size][match];
}
int main(){
int arr[] = {4, 2, 8, 10, 3, 4, 4};
int match = 2;
int size = sizeof(arr)/sizeof(arr[0]);
cout<<"Count of number of subsets having a particular XOR value are: "<<subset_XOR(arr, size, match);
return 0;
}

## Output

If we run the above code it will generate the following output −

Count of number of subsets having a particular XOR value are − 8
Updated on 05-Jan-2021 07:25:01