Count of subsets having maximum possible XOR value - GeeksforGeeks

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: 1Explanation: 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++
Java
Python3
C#
Javascript
// C++ program for above approach#include <bits/stdc++.h>using namespace std; // Function to find subsets having maximum XORint 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 Codeint main(){    int N = 4;    vector<int> arr = { 3, 2, 1, 5 };     // Print the answer    cout << countMaxOrSubsets(arr);     return 0;}
// Java program for above approachimport java.util.*; public class GFG{   // Function to find subsets having maximum XORstatic 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 Codepublic 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.
# Python program for above approach # Function to find subsets having maximum XORdef 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 Codeif __name__ == "__main__":     N = 4    arr = [3, 2, 1, 5]     # Print the answer    print(countMaxOrSubsets(arr)) # This code is contributed by rakeshsahni
// C# program for above approachusing System; public class GFG{   // Function to find subsets having maximum XORstatic 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 Codepublic static void Main(String []args){    int []arr = { 3, 2, 1, 5 };     // Print the answer    Console.Write(countMaxOrSubsets(arr));}} // This code is contributed by 29AjayKumar
<script> // JavaScript program for above approach // Function to find subsets having maximum XORfunction 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 Codelet N = 4;let arr = [ 3, 2, 1, 5 ]; // Print the answerdocument.write(countMaxOrSubsets(arr)); // This code is contributed by Potta Lokesh </script>
2
Time Complexity: O(216) Auxiliary Space: O(1)
lokeshpotta20
rakeshsahni
samim2000
29AjayKumar
simmytarika5
Bit Algorithms
Bitwise-XOR
subset
Arrays
Bit Magic
Mathematical
Arrays
Mathematical
Bit Magic
subset
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Window Sliding Technique
Program to find sum of elements in a given array
Move all negative numbers to beginning and positive to end with constant extra space
Reversal algorithm for array rotation
Find duplicates in O(n) time and O(1) extra space | Set 1
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Cyclic Redundancy Check and Modulo-2 Division
Count set bits in an integer