Sum of bitwise OR of all possible subsets of given set


The problem statement includes printing the sum of bitwise OR of all possible subsets of a given set.

A set is a collection of data of similar type. A subset of any set is a set containing few elements of the set or all the elements of the given set. The number of subsets of any set is given by $\mathrm{2^{n}−1}$, where n is the number of elements in the given set.

For example, a={1,2,3,4,5} is the given set.

{1}. {2,3}, {1,2,3,4} and so on are called subsets of a, as they contain the elements present in a. We will be applying the bitwise OR operator on all the possible subsets of the given set and sum the resultant of all.

Syntax for OR operator

int a,b;
int c= a || b;

The operator (‘||”) will return the resultant of OR of a and b.

In this problem, we will be given an array of size N. We need to find the OR of all the possible subsets of the array and sum them. The sum of bitwise OR of all the subsets of the array will be our required output.

Let’s understand the problem with the example below.

Input

a[]={2, 4, 6}

Output

36

Explanation − We are given the array of size 3 in the input. The number of subsets of a will be $\mathrm{2^{n}−1}$, which is $\mathrm{2^{3}−1=7}$. They are {2}, {4}, {6}, {2,4}, {2,6}, {4,6} and {2,4,6}. Applying OR operation on each subset:

{2} =2

{4}=4

{6}=6

{2,4}=2 || 4=6

{2,6}= 2||6=6

{4,6}= 4||6=6

{2,4,6}= 2||4||6=6

Above are the OR of all the possible subsets of a. The sum of all results is 36, which is the required output.

In this way, we need to calculate the OR of all the subsets of a, of size n which will be equal to $\mathrm{2^{n}−1}$ and take OR of each case. The sum of the resultant of each subset after applying bitwise OR operator on them will be the answer we require.

Let’s look at the algorithm to solve the problem.

Algorithm

Since the number of possible subsets of any given array of size n can be $\mathrm{2^{n−1}}$.Therefore, generating all the possible subsets of a given array for larger values of n and then calculating the OR of all the generated subsets will take much runtime which will be the naive approach to solve the problem.

Instead an efficient approach to solve the problem can be taking the binary form of all the elements present in the array and calculating the number of subsets with the particular bit as 1 and calculating the sum.

The bitwise OR operator work in the following way:

1 || 1 = 1

0 || 0 = 0

0 || 1 = 1

1 || 0 = 1

With reference to the property of bitwise OR operator we can conclude that if any one element present in the subset with a particular bit as 1, the resultant will include that bit as 1 OR 0 is equal to 1.

We will simply calculate the number of elements present in the array with the ith bit as 0 and store them in an array of size 32 for getting the number of subsets with the ith bit as 0. For this, we will iterate in a for loop from i=0 to i<31 and then iterate in a nested loop in the given array to check the ith bit of every element present in the array.

We can check the ith bit of every element present in the array by calculating bitwise AND of the particular element and 1<<i. The AND operator returns 1 if both the bits are 1 and if either of the bits is 0, it will return 0.

Note: << is the left shift operator which left shifts the bits of the particular number. If we left shift 1 by i, the resultant will be 1*2^i.

After calculating the number of elements in the array with the ith bit as 0, we can get the number of subsets we can form using those elements using the formula $\mathrm{2^{n−1}}$ where n will be the number of elements in the array with ith bit as 0.

The sum of bitwise OR of all the subsets can be calculated by:

$\mathrm{sum=((2^{n−1})−2^{numner\:of\:elements\:with\:ith\:bit\:0}−1)*2^{i}}$

Here, n= number of elements in the given array.

i=number of bit where i ranges from 0 to 31.

If any of the elements in the subset has the ith bit 1 the resultant will have that ith bit as 1 according to the properties of the OR operator. The total number of subsets having ith bit as 1 multiplied by 2^i for every i from 0 to 31 will give the resultant sum of OR of all the subsets of array.

So subtracting the number of subsets with the ith bit as 0 of every element from the total number of subsets will give the number of subsets with the ith bit as 1.

We will use the above algorithm which can be an efficient approach to solve the problem.

Approach

The steps to implement the aforementioned algorithm in our approach in C++:

  • For calculating the sum of bitwise OR of all the subsets of the given array, we will make a function.

  • We will create an array of size 32 to store the number of elements with the particular bit as 0.

  • Iterate in a for loop from i=0 to i<32 to check the ith bit of every element present in the array. Iterating in a nested for loop to check the ith bit of each element using AND operator. If the ith bit is 0 of any element in the array, increase the value at ith index of the created array by 1 for each element with ith bit as 0.

  • Once we get the number of elements of the array with ith bit as 0, we will then iterate again in a for loop from i=0 to i<32 to calculate the sum of OR of all possible subsets.

  • Now for the ith iteration, we will calculate the number of subsets with the ith bit as 1 by subtracting the total subsets of array with the number of subsets with the ith bit as 0 and multiplying the resultant with 2^i to get the sum. We can get the number of subsets with ith bit as 0 using 2^n−1 where n will be the number of elements with the ith bit as 0 which we stored in the array that we created.

  • The sum of total bitwise OR of all the subsets of the given array may be obtained by updating the sum after every iteration to check every ith bit up to 31.

Example

//C++ code to find the sum of bitwise OR of all the subsets of the given array

#include<bits/stdc++.h>

using namespace std;

//function to calculate sum of bitwise OR of all subsets
int sum(int a[], int N){
    
    int bits[32]={0}; //to store the number of elements with ith bit as 0
    
    for(int i=0;i<32;i++){ //to check for every bit
        for(int j=0;j<N;j++){ //to check elements with ith bit as 0
        //checking ith bit of each element in the array using AND operator
        //left shift 1 by i
            if((a[j]&(1<<i))==0){
                bits[i]++; //if ith bit is 0 of the element increase bits[i] by 1
            }
        }
    }
    
    int sum=0; //to store the sum of OR of all the subsets
    
    for(int i=0;i<32;i++){ //to calculate the total sum
    //number of subsets with a element with ith bit as 1 multiplied by 2^i
        sum = sum + ((pow(2,N)-1)-(pow(2,bits[i])-1))*pow(2,i);
    }
    
    return sum; //return the sum
    
}

int main()
{
    int a[]={2, 3, 7, 10, 4};
    int N;
    
    N=sizeof(a)/sizeof(a[0]); //calculating the size of the array
    //calling the function
    cout<<"The total sum of OR of all the subsets of a : "<<sum(a,N)<<endl;
    
    int b[] = {5, 9, 25, 52, 32, 21, 15};
    N=sizeof(b)/sizeof(b[0]);
    
    cout<<"The total sum of OR of all the subsets of a : "<<sum(b,N)<<endl;

    return 0;
}

Output

The total sum of OR of all the subsets of a : 308
The total sum of OR of all the subsets of a : 6492

Time complexity− O(N) , where N is the size of the array given.

Space complexity − O(1) , as we just created an array of size 32 making space complexity O(32) which is equal to O(1). We are using constant space to solve the problem.

Conclusion

The article covered the issue of calculating the bitwise OR of all conceivable subsets of the given array. We came up with an effective technique in the article and applied that algorithm in our method to solve the problem effectively in C++ in O(N) runtime and utilising constant space as opposed to finding all the subsets of the array and determining the OR.

After reading this post, I hope you have a better understanding of the problem and the algorithm used to solve it in C++.

Updated on: 28-Aug-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements