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: 8
Explanation:
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 required set is 8.
Input: arr[] = {6}
Output: 1
Naive Approach: The simplest approach is to generate all possible non-empty subsets of the given array arr[] and store the Bitwise XOR of all subsets in a set. After generating all the subsets, print the size of the set obtained as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
unordered_set< int > s;
void countXOR( int arr[], int comb[],
int start, int end,
int index, int r)
{
if (index == r) {
int new_xor = 0;
for ( int j = 0; j < r; j++) {
new_xor ^= comb[j];
}
s.insert(new_xor);
return ;
}
for ( int i = start;
i <= end && end - i + 1 >= r - index; i++) {
comb[index] = arr[i];
countXOR(arr, comb, i + 1, end,
index + 1, r);
}
}
void maxSizeSet( int arr[], int N)
{
for ( int r = 2; r <= N; r++) {
int comb[r + 1];
countXOR(arr, comb, 0, N - 1,
0, r);
}
cout << s.size() << endl;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
maxSizeSet(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static HashSet<Integer> s;
static void countXOR( int arr[], int comb[],
int start, int end,
int index, int r)
{
if (index == r)
{
int new_xor = 0 ;
for ( int j = 0 ; j < r; j++)
{
new_xor ^= comb[j];
}
s.add(new_xor);
return ;
}
for ( int i = start;
i <= end && end - i + 1 >= r - index;
i++)
{
comb[index] = arr[i];
countXOR(arr, comb, i + 1 , end, index + 1 , r);
}
}
static void maxSizeSet( int arr[], int N)
{
for ( int r = 1 ; r <= N; r++)
{
int comb[] = new int [r + 1 ];
countXOR(arr, comb, 0 , N - 1 , 0 , r);
}
System.out.println(s.size());
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
s = new HashSet<>();
maxSizeSet(arr, N);
}
}
|
Python3
s = set ([])
def countXOR(arr, comb, start, end, index, r):
if (index = = r) :
new_xor = 0
for j in range (r):
new_xor ^ = comb[j]
s.add(new_xor)
return
i = start
while i < = end and (end - i + 1 ) > = (r - index):
comb[index] = arr[i]
countXOR(arr, comb, i + 1 , end, index + 1 , r)
i + = 1
def maxSizeSet(arr, N):
for r in range ( 2 , N + 1 ):
comb = [ 0 ] * (r + 1 )
countXOR(arr, comb, 0 , N - 1 , 0 , r)
print ( len (s))
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
maxSizeSet(arr, N)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static HashSet< int > s;
static void countXOR( int []arr, int []comb,
int start, int end,
int index, int r)
{
if (index == r)
{
int new_xor = 0;
for ( int j = 0; j < r; j++)
{
new_xor ^= comb[j];
}
s.Add(new_xor);
return ;
}
for ( int i = start;
i <= end && end - i + 1 >= r - index;
i++)
{
comb[index] = arr[i];
countXOR(arr, comb, i + 1, end, index + 1, r);
}
}
static void maxSizeSet( int []arr, int N)
{
for ( int r = 1; r <= N; r++)
{
int []comb = new int [r + 1];
countXOR(arr, comb, 0, N - 1, 0, r);
}
Console.WriteLine(s.Count);
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
s = new HashSet< int >();
maxSizeSet(arr, N);
}
}
|
Javascript
<script>
let s;
function countXOR(arr,comb,start,end,index,r)
{
if (index == r)
{
let new_xor = 0;
for (let j = 0; j < r; j++)
{
new_xor ^= comb[j];
}
s.add(new_xor);
return ;
}
for (let i = start;
i <= end && end - i + 1 >= r - index;
i++)
{
comb[index] = arr[i];
countXOR(arr, comb, i + 1, end, index + 1, r);
}
}
function maxSizeSet(arr,N)
{
for (let r = 1; r <= N; r++)
{
let comb = new Array(r + 1);
countXOR(arr, comb, 0, N - 1, 0, r);
}
document.write(s.size);
}
let arr=[1, 2, 3, 4, 5 ];
let N = arr.length;
s = new Set();
maxSizeSet(arr, N);
</script>
|
Time Complexity: O(NN)
Auxiliary Space: O(N)
Efficient approach: The above approach can be optimized by using the Greedy Approach and Gaussian Elimination. Follow the steps below to solve the problem:
- Initialize an auxiliary array, say dp[] of size 20, that stores the mask of each array element and initializes it with 0.
- Initialize a variable, say ans as 0, to store the size of the array dp[].
- Traverse the given array arr[] and for each array element arr[], perform the following steps:
- Initialize a variable, say mask as arr[i], to check the position of the most significant set bit.
- If the ith bit from the right of arr[i] is set and dp[i] is 0, then update the array dp[i] as 2i and increment the value of ans by 1 and break out of the loop.
- Otherwise, update the value of the mask as the Bitwise XOR of the mask and dp[i].
- After completing the above steps, print the value of 2ans as the resultant size of the required set of elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int const size = 20;
int dp[size];
int ans;
void insertVector( int mask)
{
for ( int i = 0; i < 20; i++) {
if ((mask & 1 << i) == 0)
continue ;
if (!dp[i]) {
dp[i] = mask;
++ans;
return ;
}
mask ^= dp[i];
}
}
void maxSizeSet( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
insertVector(arr[i]);
}
cout << (1 << ans) << endl;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
maxSizeSet(arr, N);
return 0;
}
|
Java
class GFG{
final static int size = 20 ;
static int [] dp = new int [size];
static int ans;
static void insertVector( int mask)
{
for ( int i = 0 ; i < 20 ; i++) {
if ((mask & 1 << i) == 0 )
continue ;
if (dp[i]== 0 ) {
dp[i] = mask;
++ans;
return ;
}
mask ^= dp[i];
}
}
static void maxSizeSet( int [] arr, int N)
{
for ( int i = 0 ; i < N; i++) {
insertVector(arr[i]);
}
System.out.println( 1 <<ans);
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
maxSizeSet(arr, N);
}
}
|
Python3
dp = [ 0 ] * 20
ans = 0
def insertVector(mask):
global dp, ans
for i in range ( 20 ):
if ((mask & 1 << i) = = 0 ):
continue
if ( not dp[i]):
dp[i] = mask
ans + = 1
return
mask ^ = dp[i]
def maxSizeSet(arr, N):
for i in range (N):
insertVector(arr[i])
print (( 1 << ans))
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
maxSizeSet(arr, N)
|
C#
using System;
class GFG{
static int size = 20;
static int [] dp = new int [size];
static int ans;
static void insertVector( int mask)
{
for ( int i = 0; i < 20; i++)
{
if ((mask & 1 << i) == 0)
continue ;
if (dp[i] == 0)
{
dp[i] = mask;
++ans;
return ;
}
mask ^= dp[i];
}
}
static void maxSizeSet( int [] arr, int N)
{
for ( int i = 0; i < N; i++)
{
insertVector(arr[i]);
}
Console.WriteLine(1 << ans);
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
maxSizeSet(arr, N);
}
}
|
Javascript
<script>
let size = 20;
var dp = new Array(size).fill(0);
let ans = 0;
function insertVector(mask)
{
for (let i = 0; i < 20; i++)
{
if ((mask & 1 << i) == 0)
continue ;
if (dp[i] == 0)
{
dp[i] = mask;
++ans;
return ;
}
mask ^= dp[i];
}
}
function maxSizeSet(arr, N)
{
for (let i = 0; i < N; i++)
{
insertVector(arr[i]);
}
document.write(1<<ans);
}
let arr = [ 1, 2, 3, 4, 5 ];
let N = arr.length;
maxSizeSet(arr, N);
</script>
|
Time Complexity: O(M * N)
Auxiliary Space: O(M * N)