Open In App

Count of subsets having maximum possible XOR value

Last Updated : 17 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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)



Similar Reads

Count of subarrays in range [L, R] having XOR + 1 equal to XOR (XOR) 1 for M queries
Given an array, arr[] of N positive integers and M queries which consist of two integers [Li, Ri] where 1 ? Li ? Ri ? N. For each query, find the number of subarrays in range [Li, Ri] for which (X+1)=(X?1) where X denotes the xor of a subarray. Input: arr[]= {1, 2, 9, 8, 7}, queries[] = {{1, 5}, {3, 4}}Output: 6 1Explanation: Query 1: L=1, R=5: sub
12 min read
Count no. of ordered subsets having a particular XOR value
Given an array arr[] of n elements and a number K, find the number of ordered subsets of arr[] having XOR of elements as K This is a modified version of this problem. So it is recommended to try that problem before.Examples: Input: arr[] = {6, 9, 4, 2}, k = 6 Output: 2 The subsets are {4, 2}, {2, 4} and {6}Input: arr[] = {1, 2, 3}, k = 1 Output: 4
11 min read
Count number of subsets having a particular XOR value
Given an array arr[] of n numbers and a number K, find the number of subsets of arr[] having XOR of elements as KExamples : Input: arr[] = {6, 9, 4,2}, k = 6Output: 2The subsets are {4, 2} and {6}Input: arr[] = {1, 2, 3, 4, 5}, k = 4Output: 4The subsets are {1, 5}, {4}, {1, 2, 3, 4} and {2, 3, 5}We strongly recommend that you click here and practic
26 min read
Split array into maximum possible subsets having product of their length with the maximum element at least K
Given an array arr[] consisting of N integers and a positive integer K, the task is to maximize the number of subsets having a product of its size and its maximum element at least K by dividing array element into disjoint subsets. Examples: Input: N = 5, K = 4, arr[] = {1, 5, 4, 2, 3}Output: 3Explanation:The array can be split into 3 possible subse
6 min read
Maximum XOR value of maximum and second maximum element among all possible subarrays
Given an array arr[] of N distinct positive integers, let's denote max(i, j) and secondMax(i, j) as the maximum and the second maximum element of the subarray arr[i...j]. The task is to find the maximum value of max(i, j) XOR secondMax(i, j) for all possible values of i and j. Note that the size of the subarray must be at least two.Examples: Input:
5 min read
Count distinct possible Bitwise XOR values of subsets of an array
Given an array arr[] consisting of N integers, the task is to find the size of the set S such that Bitwise XOR of any subset of the array arr[] exists in the set S. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 8Explanation:All possible Bitwise XOR values of subsets of the array arr[] are {0, 1, 2, 3, 4, 5, 6, 7}.Therefore, the size of the requir
14 min read
Given a set, find XOR of the XOR's of all subsets.
The question is to find XOR of the XOR's of all subsets. i.e if the set is {1,2,3}. All subsets are : [{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]. Find the XOR of each of the subset and then find the XOR of every subset result.We strongly recommend you to minimize your browser and try this yourself first.This is a very simple question to sol
5 min read
Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
Given an integer N, the task is to find the largest number M, where (M &lt; N), such that N(XOR)M is equal to N(OR)M i.e. (N ^ M) = (N | M). Examples: Input: N = 5 Output: 2 5 ^ 4 = 1 and 5 | 4 = 5. Therefore, XOR and OR between them are not equal. 5 ^ 3 = 6 and 5 | 3 = 7. Therefore, XOR and OR between them are not equal. 5 ^ 2 = 7 and 5 | 2 = 7. T
5 min read
Check if it is possible to construct an Array of size N having sum as S and XOR value as X
Given three numbers N, S and X the task is to find if it is possible to construct a sequence A of length N, where each A[i] &gt;= 0 for 1&lt;=i&lt;=N and the sum of all numbers in a sequence is equal to S, and the bit-wise XOR of sequence equals to X. Examples: Input: N = 3, S = 10, X = 4 Output: YesExplanation: One of the sequence possible is {4,
10 min read
Divide array in two Subsets such that sum of square of sum of both subsets is maximum
Given an integer array arr[], the task is to divide this array into two non-empty subsets such that the sum of the square of the sum of both the subsets is maximum and sizes of both the subsets must not differ by more than 1.Examples: Input: arr[] = {1, 2, 3} Output: 26 Explanation: Sum of Subset Pairs are as follows (1)2 + (2 + 3)2 = 26 (2)2 + (1
6 min read