Count of subsets having maximum possible XOR value
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++
#include <bits/stdc++.h>
using namespace std;
int countMaxOrSubsets(vector< int >& nums)
{
long long n = nums.size();
long long maxXorVal = 0;
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]);
}
}
maxXorVal = max(maxXorVal, xorVal);
}
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;
}
int main()
{
int N = 4;
vector< int > arr = { 3, 2, 1, 5 };
cout << countMaxOrSubsets(arr);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int countMaxOrSubsets( int []nums)
{
long n = nums.length;
long maxXorVal = 0 ;
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]);
}
}
maxXorVal = Math.max(maxXorVal, xorVal);
}
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;
}
public static void main(String args[])
{
int N = 4 ;
int []arr = { 3 , 2 , 1 , 5 };
System.out.print(countMaxOrSubsets(arr));
}
}
|
Python3
def countMaxOrSubsets(nums):
n = len (nums)
maxXorVal = 0
for i in range ( 0 , ( 1 << n)):
xorVal = 0
for j in range ( 0 , n):
if (i & ( 1 << j)):
xorVal = (xorVal ^ nums[j])
maxXorVal = max (maxXorVal, xorVal)
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
if __name__ = = "__main__" :
N = 4
arr = [ 3 , 2 , 1 , 5 ]
print (countMaxOrSubsets(arr))
|
C#
using System;
public class GFG
{
static int countMaxOrSubsets( int []nums)
{
int n = nums.Length;
int maxXorVal = 0;
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]);
}
}
maxXorVal = ( int )Math.Max(maxXorVal, xorVal);
}
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;
}
public static void Main(String []args)
{
int []arr = { 3, 2, 1, 5 };
Console.Write(countMaxOrSubsets(arr));
}
}
|
Javascript
<script>
function countMaxOrSubsets(nums)
{
let n = nums.length;
let maxXorVal = 0;
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]);
}
}
maxXorVal = Math.max(maxXorVal, xorVal);
}
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;
}
let N = 4;
let arr = [ 3, 2, 1, 5 ];
document.write(countMaxOrSubsets(arr));
</script>
|
Time Complexity: O(216)
Auxiliary Space: O(1)
Last Updated :
17 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...