Skip to content
Related Articles

Related Articles

Improve Article

Sum of bitwise OR of all possible subsets of given set

  • Difficulty Level : Hard
  • Last Updated : 19 May, 2021

Given an array arr[] of size n, we need to find sum of all the values that comes from ORing all the elements of the subsets. 
Prerequisites : Subset Sum of given set
Examples : 
 

Input :  arr[] = {1, 2, 3}
Output : 18
Total Subsets = 23 -1= 7 
1 = 1
2 = 2
3 = 3
1 | 2 = 3
1 | 3 = 3
2 | 3 = 3
1 | 2 | 3 = 3
0(empty subset)
Now SUM of all these ORs = 1 + 2 + 3 + 3 +
                            3 + 3 + 3
                          = 18

Input : arr[] = {1, 2, 3}
Output : 18
 

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

A Naive approach is to take the OR all possible combination of array[] elements and then perform the summation of all values. Time complexity of this approach grows exponentially so it would not be better for large value of n.
An Efficient approach is to find the pattern with respect to the property of OR. Now again consider the subset in binary form like: 
 



    1 = 001
    2 = 010
    3 = 011
1 | 2 = 011
1 | 3 = 011
2 | 3 = 011
1|2|3 = 011

Instead of taking the OR of all possible elements of array, Here we will consider all possible subset with ith bit 1. 
Now, consider the ith bit in all the resultant ORs, it is zero only if all the ith bit of elements in the subset is 0. 
Number of subset with ith bit 1 = total possible subsets – subsets with all ith bit 0. Here, total subsets = 2^n – 1 and subsets with all ith bits 0 = 2^( count of zeros at ith bit of all the elements of array) – 1. Now, Total subset OR with ith bit 1 = (2^n-1)-(2^(count of zeros at ith bit)-1). Total value contributed by those bits with value 1 = total subset OR with ith bit 1 *(2^i). 
Now, total sum = (total subset with ith bit 1) * 2^i + (total subset with i+1th bit 1) * 2^(i+1) + ……… + (total subset with 32 bit 1) * 2^32.
 

C++




// CPP code to find the OR_SUM
#include <bits/stdc++.h>
using namespace std;
 
#define INT_SIZE 32
 
// function to find the OR_SUM
int ORsum(int arr[], int n)
{
    // create an array of size 32
    // and store the sum of bits
    // with value 0 at every index.
    int zerocnt[INT_SIZE] = { 0 };
 
    for (int i = 0; i < INT_SIZE; i++)    
        for (int j = 0; j < n; j++)       
            if (!(arr[j] & 1 << i))
                zerocnt[i] += 1;           
     
    // for each index the OR sum contributed
    // by that bit of subset will be 2^(bit index)
    // now the OR of the bits is 0 only if
    // all the ith bit of the elements in subset
    // is 0.
    int ans = 0;
    for (int i = 0; i < INT_SIZE; i++)
    {
        ans += ((pow(2, n) - 1) -
               (pow(2, zerocnt[i]) - 1)) *
                pow(2, i);
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3 };
    int size = sizeof(arr) / sizeof(arr[0]);
    cout << ORsum(arr, size);
    return 0;
}

Java




// Java code to find
// the OR_SUM
import java.io.*;
 
class GFG {
     
static int INT_SIZE = 32;
 
    // function to find
    // the OR_SUM
    static int ORsum(int []arr, int n)
    {
         
        // create an array of size 32
        // and store the sum of bits
        // with value 0 at every index.
        int zerocnt[] = new int[INT_SIZE] ;
     
        for (int i = 0; i < INT_SIZE; i++)    
            for (int j = 0; j < n; j++)    
                if ((arr[j] & 1 << i) == 0)
                    zerocnt[i] += 1;        
         
        // for each index the OR
        // sum contributed by that
        // bit of subset will be
        // 2^(bit index) now the OR
        // of the bits is 0 only if
        // all the ith bit of the 
        // elements in subset is 0.
        int ans = 0;
        for (int i = 0; i < INT_SIZE; i++)
        {
            ans += ((Math.pow(2, n) - 1) -
                (Math.pow(2, zerocnt[i]) - 1)) *
                                 Math.pow(2, i);
        }
     
        return ans;
         
    }
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3 };
        int size = arr.length;
        System.out.println(ORsum(arr, size));
         
    }
}
 
// This code is contributed by Sam007

Python3




INT_SIZE = 32
 
# function to find the OR_SUM
def ORsum(arr, n):
    # create an array of size 32
    # and store the sum of bits
    # with value 0 at every index.
    zerocnt = [0 for i in range(INT_SIZE)]
 
    for i in range(INT_SIZE):   
        for j in range(n):   
            if not (arr[j] & (1 << i)):
                zerocnt[i] += 1           
     
    # for each index the OR sum contributed
    # by that bit of subset will be 2^(bit index)
    # now the OR of the bits is 0 only if
    # all the ith bit of the elements in subset
    # is 0.
    ans = 0
    for i in range(INT_SIZE):
        ans += ((2 ** n - 1) - (2 ** zerocnt[i] - 1)) * 2 ** i
 
    return ans
 
# Driver code
 
if __name__ == "__main__":
    arr= [1, 2, 3]
    size = len(arr)
    print(ORsum(arr, size))
 
# This code is contributed by vaibhav29498

C#




// C# code to find
// the OR_SUM
using System;
 
class GFG {
     
static int INT_SIZE = 32;
 
    // function to find
    // the OR_SUM
    static int ORsum(int []arr, int n)
    {
         
        // create an array of size 32
        // and store the sum of bits
        // with value 0 at every index.
        int []zerocnt = new int[INT_SIZE] ;
     
        for (int i = 0; i < INT_SIZE; i++)    
            for (int j = 0; j < n; j++)    
                if ((arr[j] & 1 << i) == 0)
                    zerocnt[i] += 1;        
         
        // for each index the OR
        // sum contributed by that
        // bit of subset will be
        // 2^(bit index) now the OR
        // of the bits is 0 only if
        // all the ith bit of the
        // elements in subset is 0.
        int ans = 0;
        for (int i = 0; i < INT_SIZE; i++)
        {
            ans += (int)(((Math.Pow(2, n) - 1) -
                 (Math.Pow(2, zerocnt[i]) - 1)) *
                                Math.Pow(2, i));
        }
     
        return ans;
         
    }
     
    // Driver Code
    public static void Main()
    {
        int []arr = {1, 2, 3};
        int size = arr.Length;
        Console.Write(ORsum(arr, size));
         
    }
}
 
// This code is contributed by nitin mittal

PHP




<?php
// PHP code to find the OR_SUM
 
$INT_SIZE = 32;
 
// function to find the OR_SUM
function ORsum(&$arr, $n)
{
    global $INT_SIZE;
     
    // create an array of size 32
    // and store the sum of bits
    // with value 0 at every index.
    $zerocnt = array_fill(0, $INT_SIZE, NULL);
 
    for ($i = 0; $i < $INT_SIZE; $i++)    
        for ($j = 0; $j < $n; $j++)    
            if (!($arr[$j] & 1 << $i))
                $zerocnt[$i] += 1;        
     
    // for each index the OR sum contributed
    // by that bit of subset will be 2^(bit index)
    // now the OR of the bits is 0 only if
    // all the ith bit of the elements in
    // subset is 0.
    $ans = 0;
    for ($i = 0; $i < $INT_SIZE; $i++)
    {
        $ans += ((pow(2, $n) - 1) -
                 (pow(2, $zerocnt[$i]) - 1)) *
                  pow(2, $i);
    }
 
    return $ans;
}
 
// Driver code
$arr = array(1, 2, 3);
$size = sizeof($arr);
echo ORsum($arr, $size);
 
// This code is contributed by ita_c
?>

Javascript




<script>
// JavaScript code to find the OR_SUM
 
let INT_SIZE = 32;
// function to find the OR_SUM
function ORsum(arr, n)
{
    // create an array of size 32
    // and store the sum of bits
    // with value 0 at every index.
    let zerocnt = new Uint8Array(INT_SIZE);
 
    for (let i = 0; i < INT_SIZE; i++)    
        for (let j = 0; j < n; j++)        
            if (!(arr[j] & 1 << i))
                zerocnt[i] += 1;            
     
    // for each index the OR sum contributed
    // by that bit of subset will be 2^(bit index)
    // now the OR of the bits is 0 only if
    // all the ith bit of the elements in subset
    // is 0.
    let ans = 0;
    for (let i = 0; i < INT_SIZE; i++)
    {
        ans += ((Math.pow(2, n) - 1) -
            (Math.pow(2, zerocnt[i]) - 1)) *
                Math.pow(2, i);
    }
 
    return ans;
}
 
// Driver code
 
    let arr = [ 1, 2, 3 ];
    let size = arr.length;
    document.write(ORsum(arr, size));
 
// This code is contributed by Surbhi Tyagi.
 
</script>
Output: 
18

 

Time complexity: O(n) 
Auxiliary space: O(n)
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :