Sum of bitwise OR of all possible subsets of given set
Last Updated :
11 Jul, 2025
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
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.
Implementation:
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>
Complexity Analysis:
- Time complexity: O(n)
- Auxiliary space: O(n)
Similar Reads
Sum of bitwise AND of all possible subsets of given set Given an array, we need to calculate the Sum of Bit-wise AND of all possible subsets of the given array. Examples: Input : 1 2 3 Output : 9 For [1, 2, 3], all possible subsets are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} Bitwise AND of these subsets are, 1 + 2 + 3 + 0 + 1 + 2 + 0 = 9. So, th
7 min read
Print sums of all subsets of a given set Given an array of integers, print sums of all subsets in it. Output sums can be printed in any order.Examples : Input: arr[] = {2, 3}Output: 0 2 3 5Explanation: All subsets of this array are - {{}, {2}, {3}, {2, 3}}, having sums - 0, 2, 3 and 5 respectively.Input: arr[] = {2, 4, 5}Output: 0 2 4 5 6
10 min read
Sum of all subsets of a given size (=K) Given an array arr[] consisting of N integers and a positive integer K, the task is to find the sum of all the subsets of size K. Examples: Input: arr[] = {1, 2, 4, 5}, K = 2Output: 36Explanation:The subsets of size K(= 2) are = {1, 2}, {1, 4}, {1, 5}, {2, 4}, {2, 5}, {4, 5}. Now, the sum of all sub
7 min read
Bitwise OR of sum of all subsequences of an array Given an array arr[] of length N, the task is to find the Bitwise OR of the sum of all possible subsequences from the given array. Examples: Input: arr[] = {4, 2, 5}Output: 15Explanation: All subsequences from the given array and their corresponding sums:{4} - 4{2} - 2{5} - 5{4, 2} - 6{4, 5} - 9{2,
6 min read
Sum of XOR of all possible subsets Given an array of size n, the task is to find the sum of all the values that come from XORing all the elements of the subsets. Examples:Input: arr[] = [1, 2, 3]Output: 28Explanation: Subsets are: [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]0(empty subset)1 = 12 = 23 = 31 ^ 2 = 31 ^ 3 = 22
11 min read
Sum of Bitwise-OR of all Submatrices Given a NxN matrix, the task is to find the sum of bit-wise OR of all of its rectangular sub-matrices.Examples: Input : arr[][] = {{1, 0, 0}, {0, 0, 0}, {0, 0, 0}} Output : 9 Explanation: All the submatrices starting from the index (0, 0) will have OR value as 1. Thus, ans = 9 Input : arr[][] = {{9,
14 min read