Skip to content
Related Articles

Related Articles

Count of subsets having maximum possible XOR value

Improve Article
Save Article
Like Article
  • Last Updated : 17 Jan, 2022

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: 

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

Java




// Java program for above approach
import java.util.*;
 
public class GFG
{
   
// Function to find subsets having maximum XOR
static int countMaxOrSubsets(int []nums)
{
    // Store the size of arr[]
    long n = nums.length;
 
    // To store maximum possible
    // bitwise XOR subset in arr[]
    long maxXorVal = 0;
 
    // Find the maximum bitwise xor value
    for (int i = 0; i < (1 << n); i++) {
 
        long xorVal = 0;
        for (int j = 0; j < n; j++) {
 
            if ((i & (1 << j)) == 0) {
                xorVal = (xorVal ^ nums[j]);
            }
        }
 
        // Take maximum of each value
        maxXorVal = Math.max(maxXorVal, xorVal);
    }
 
    // Count the number
    // of subsets having bitwise
    // XOR value as maxXorVal
    long count = 0;
 
    for (int i = 0; i < (1 << n); i++) {
        long val = 0;
        for (int j = 0; j < n; j++) {
            if ((i & (1 << j)) == 0) {
                val = (val ^ nums[j]);
            }
        }
 
        if (val == maxXorVal) {
            count++;
        }
    }
    return (int)count;
}
 
// Driver Code
public static void main(String args[])
{
    int N = 4;
    int []arr = { 3, 2, 1, 5 };
 
    // Print the answer
    System.out.print(countMaxOrSubsets(arr));
}
}
 
// This code is contributed by Samim Hossain Mondal.

Python3




# Python program for above approach
 
# Function to find subsets having maximum XOR
def countMaxOrSubsets(nums):
 
    # Store the size of arr[]
    n = len(nums)
 
    # To store maximum possible
    # bitwise XOR subset in arr[]
    maxXorVal = 0
 
    # Find the maximum bitwise xor value
    for i in range(0, (1 << n)):
 
        xorVal = 0
        for j in range(0, n):
 
            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
    count = 0
 
    for i in range(0, (1 << n)):
        val = 0
        for j in range(0, n):
            if (i & (1 << j)):
                val = (val ^ nums[j])
 
        if (val == maxXorVal):
            count += 1
    return count
 
# Driver Code
if __name__ == "__main__":
 
    N = 4
    arr = [3, 2, 1, 5]
 
    # Print the answer
    print(countMaxOrSubsets(arr))
 
# This code is contributed by rakeshsahni

C#




// C# program for above approach
using System;
 
public class GFG
{
   
// Function to find subsets having maximum XOR
static int countMaxOrSubsets(int []nums)
{
   
    // Store the size of []arr
    int n = nums.Length;
 
    // To store maximum possible
    // bitwise XOR subset in []arr
    int maxXorVal = 0;
 
    // Find the maximum bitwise xor value
    for (int i = 0; i < (1 << n); i++) {
 
        long xorVal = 0;
        for (int j = 0; j < n; j++) {
 
            if ((i & (1 << j)) == 0) {
                xorVal = (xorVal ^ nums[j]);
            }
        }
 
        // Take maximum of each value
        maxXorVal = (int)Math.Max(maxXorVal, xorVal);
    }
 
    // Count the number
    // of subsets having bitwise
    // XOR value as maxXorVal
    long count = 0;
 
    for (int i = 0; i < (1 << n); i++) {
        long val = 0;
        for (int j = 0; j < n; j++) {
            if ((i & (1 << j)) == 0) {
                val = (val ^ nums[j]);
            }
        }
 
        if (val == maxXorVal) {
            count++;
        }
    }
    return (int)count;
}
 
// Driver Code
public static void Main(String []args)
{
    int []arr = { 3, 2, 1, 5 };
 
    // Print the answer
    Console.Write(countMaxOrSubsets(arr));
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
 
// JavaScript program for above approach
 
// Function to find subsets having maximum XOR
function countMaxOrSubsets(nums)
{
     
    // Store the size of arr[]
    let n = nums.length;
 
    // To store maximum possible
    // bitwise XOR subset in arr[]
    let maxXorVal = 0;
 
    // Find the maximum bitwise xor value
    for(let i = 0; i < (1 << n); i++)
    {
        let xorVal = 0;
        for(let j = 0; j < n; j++)
        {
            if (i & (1 << j))
            {
                xorVal = (xorVal ^ nums[j]);
            }
        }
 
        // Take maximum of each value
        maxXorVal = Math.max(maxXorVal, xorVal);
    }
 
    // Count the number
    // of subsets having bitwise
    // XOR value as maxXorVal
    let count = 0;
 
    for(let i = 0; i < (1 << n); i++)
    {
        let val = 0;
        for (let j = 0; j < n; j++)
        {
            if (i & (1 << j))
            {
                val = (val ^ nums[j]);
            }
        }
 
        if (val == maxXorVal)
        {
            count++;
        }
    }
    return count;
}
 
// Driver Code
let N = 4;
let arr = [ 3, 2, 1, 5 ];
 
// Print the answer
document.write(countMaxOrSubsets(arr));
 
// This code is contributed by Potta Lokesh
 
</script>
Output
2

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!