- Related Questions & Answers
- Count subsets having distinct even numbers in C++
- Sum of XOR of all possible subsets in C++
- Count n digit numbers not having a particular digit in C++
- Count number of subsets of a set with GCD equal to a given number in C++
- Count number of times value appears in particular column in MySQL?
- Count number of ways to partition a set into k subsets in C++
- Count of cyclic permutations having XOR with other binary string as 0 in C++
- How to count the number of columns having specific value in MySQL?
- Count pairs with Bitwise XOR as EVEN number in C++
- Count pairs with Bitwise XOR as ODD number in C++
- Find a value whose XOR with given number is maximum in C++
- Maximum XOR value of a pair from a range in C++
- Count number of subsets whose median is also present in the same subset in C++
- Count smaller numbers whose XOR with n produces greater value in C++
- Minimum XOR Value Pair in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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**

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

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

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

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

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

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.

#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; }

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

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

Advertisements