The Wayback Machine - https://web.archive.org/web/20211208102653/https://www.geeksforgeeks.org/count-of-subsets-having-maximum-possible-xor-value/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count of subsets having maximum possible XOR value

  • Last Updated : 08 Dec, 2021

Given an array arr[] consisting of N positive integers. The task is to count the number of different non-empty subsets of arr[] having maximum bitwise XOR. 

Examples: 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = {3, 1}
Output: 1
Explanation: The maximum possible bitwise XOR of a subset is 3. 
In arr[] there is only one subset with bitwise XOR as 3 which is {3}.
Therefore, 1 is the answer. 



Input: arr[] = {3, 2, 1, 5}
Output: 2

Approach: This problem can be solved by using Bit Masking. Follow the steps below to solve the given problem. 

  • Initialize a variable say maxXorVal = 0, to store the maximum possible bitwise XOR of a subset in arr[].
  • Traverse the array arr[] to find the value of maxXorVal. 
  • Initialize a variable say countSubsets = 0, to count the number of subsets with maximum bitwise XOR. 
  • After that count the number of subsets with the value maxXorVal. 
  • Return countSubsets as the final answer. 

Below is the implementation of the above approach. 

C++




// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find subsets having maximum XOR
int countMaxOrSubsets(vector<int>& nums)
{
    // Store the size of arr[]
    long long n = nums.size();
  
    // To sotre maximum possible
    // bitwise XOR subset in arr[]
    long long maxXorVal = 0;
  
    // Find the maximum bitwise xor value
    for (int i = 0; i < (1 << n); i++) {
  
        long long xorVal = 0;
        for (int j = 0; j < n; j++) {
  
            if (i & (1 << j)) {
                xorVal = (xorVal ^ nums[j]);
            }
        }
  
        // Take maximum of each value
        maxXorVal = max(maxXorVal, xorVal);
    }
  
    // Count the number
    // of subsets having bitwise
    // XOR value as maxXorVal
    long long count = 0;
  
    for (int i = 0; i < (1 << n); i++) {
        long long val = 0;
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                val = (val ^ nums[j]);
            }
        }
  
        if (val == maxXorVal) {
            count++;
        }
    }
    return count;
}
  
// Driver Code
int main()
{
    int N = 4;
    vector<int> arr = { 3, 2, 1, 5 };
  
    // Print the answer
    cout << countMaxOrSubsets(arr);
  
    return 0;
}
Output
2

Time Complexity: O(216)
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!