Sum of bitwise AND of all possible subsets of given set
Last Updated :
11 Jul, 2025
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, the answer would be 9.
Input : 1 2 3 4
Output : 13
Refer to this Post for Count Set Bit
Naive Approach, we can produce all subsets using Power Set then calculate Bit-wise AND sum of all subsets.
In a Better approach, we are trying to calculate which array element is responsible for producing the sum into a subset.
Let's start with the least significant bit. To remove the contribution from other bits, we calculate number AND bit for all numbers in the set. Any subset of this that contains a 0 will not give any contribution. All nonempty subsets that only consist of 1’s will give 1 in contribution. In total there will be 2^n - 1 such subset each giving 1 in contribution. The same goes for the other bit. We get [0, 2, 2], 3 subset each giving 2. Total 3*1 + 3*2 = 9
Array = {1, 2, 3}
Binary representation
positions 2 1 0
1 0 0 1
2 0 1 0
3 0 1 1
[ 0 2 2 ]
Count set bit for each position
[ 0 3 3 ] subset produced by each
position 2^n -1 i.e. n is total sum
for each position [ 0, 3*2^1, 3*2^0 ]
Now calculate the sum by multiplying
the position value i.e 2^0, 2^1 ... .
0 + 6 + 3 = 9
Implementation:
CPP
// C++ program to calculate sum of Bit-wise
// and sum of all subsets of an array
#include <bits/stdc++.h>
using namespace std;
#define BITS 32
int andSum(int arr[], int n)
{
int ans = 0;
// assuming representation of each element is
// in 32 bit
for (int i = 0; i < BITS; i++) {
int countSetBits = 0;
// iterating array element
for (int j = 0; j < n; j++) {
// Counting the set bit of array in
// ith position
if (arr[j] & (1 << i))
countSetBits++;
}
// counting subset which produce sum when
// particular bit position is set.
int subset = (1 << countSetBits) - 1;
// multiplying every position subset with 2^i
// to count the sum.
subset = (subset * (1 << i));
ans += subset;
}
return ans;
}
// Drivers code
int main()
{
int arr[] = { 1, 2, 3};
int size = sizeof(arr) / sizeof(arr[0]);
cout << andSum(arr, size);
return 0;
}
Java
// Java program to calculate sum of Bit-wise
// and sum of all subsets of an array
class GFG {
static final int BITS = 32;
static int andSum(int arr[], int n)
{
int ans = 0;
// assuming representation of each
// element is in 32 bit
for (int i = 0; i < BITS; i++) {
int countSetBits = 0;
// iterating array element
for (int j = 0; j < n; j++) {
// Counting the set bit of
// array in ith position
if ((arr[j] & (1 << i)) != 0)
countSetBits++;
}
// counting subset which produce
// sum when particular bit
// position is set.
int subset = (1 << countSetBits) - 1;
// multiplying every position
// subset with 2^i to count the
// sum.
subset = (subset * (1 << i));
ans += subset;
}
return ans;
}
// Drivers code
public static void main(String args[])
{
int arr[] = { 1, 2, 3};
int size = 3;
System.out.println (andSum(arr, size));
}
}
// This code is contributed by Arnab Kundu.
Python3
# Python3 program to calculate sum of
# Bit-wise and sum of all subsets of
# an array
BITS = 32;
def andSum(arr, n):
ans = 0
# assuming representation
# of each element is
# in 32 bit
for i in range(0, BITS):
countSetBits = 0
# iterating array element
for j in range(0, n) :
# Counting the set bit
# of array in ith
# position
if (arr[j] & (1 << i)) :
countSetBits = (countSetBits
+ 1)
# counting subset which
# produce sum when
# particular bit position
# is set.
subset = ((1 << countSetBits)
- 1)
# multiplying every position
# subset with 2^i to count
# the sum.
subset = (subset * (1 << i))
ans = ans + subset
return ans
# Driver code
arr = [1, 2, 3]
size = len(arr)
print (andSum(arr, size))
# This code is contributed by
# Manish Shaw (manishshaw1)
C#
// C# program to calculate sum of Bit-wise
// and sum of all subsets of an array
using System;
class GFG {
static int BITS = 32;
static int andSum(int[] arr, int n)
{
int ans = 0;
// assuming representation of each
// element is in 32 bit
for (int i = 0; i < BITS; i++) {
int countSetBits = 0;
// iterating array element
for (int j = 0; j < n; j++) {
// Counting the set bit of
// array in ith position
if ((arr[j] & (1 << i)) != 0)
countSetBits++;
}
// counting subset which produce
// sum when particular bit position
// is set.
int subset = (1 << countSetBits) - 1;
// multiplying every position subset
// with 2^i to count the sum.
subset = (subset * (1 << i));
ans += subset;
}
return ans;
}
// Drivers code
static public void Main()
{
int []arr = { 1, 2, 3};
int size = 3;
Console.WriteLine (andSum(arr, size));
}
}
// This code is contributed by Arnab Kundu.
PHP
<?php
// PHP program to calculate sum of Bit-wise
// and sum of all subsets of an array
$BITS = 32;
function andSum( $arr, $n)
{
global $BITS;
$ans = 0;
// assuming representation
// of each element is
// in 32 bit
for($i = 0; $i < $BITS; $i++)
{
$countSetBits = 0;
// iterating array element
for ( $j = 0; $j < $n; $j++) {
// Counting the set bit
// of array in ith position
if ($arr[$j] & (1 << $i))
$countSetBits++;
}
// counting subset which
// produce sum when
// particular bit position
// is set.
$subset = (1 << $countSetBits) - 1;
// multiplying every position
// subset with 2^i to count
// the sum.
$subset = ($subset * (1 << $i));
$ans += $subset;
}
return $ans;
}
// Driver code
$arr = array(1, 2, 3);
$size = count($arr);
echo andSum($arr, $size);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// javascript program to calculate sum of Bit-wise
// and sum of all subsets of an array
var BITS = 32;
function andSum(arr , n) {
var ans = 0;
// assuming representation of each
// element is in 32 bit
for (i = 0; i < BITS; i++) {
var countSetBits = 0;
// iterating array element
for (j = 0; j < n; j++) {
// Counting the set bit of
// array in ith position
if ((arr[j] & (1 << i)) != 0)
countSetBits++;
}
// counting subset which produce
// sum when particular bit
// position is set.
var subset = (1 << countSetBits) - 1;
// multiplying every position
// subset with 2^i to count the
// sum.
subset = (subset * (1 << i));
ans += subset;
}
return ans;
}
// Drivers code
var arr = [ 1, 2, 3 ];
var size = 3;
document.write(andSum(arr, size));
// This code contributed by gauravrajput1
</script>
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Similar Reads
Sum of bitwise OR of all possible subsets of given set 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
8 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 subsets of all the subsets of an array | O(N) Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset wi
7 min read
Sum of subsets of all the subsets of an array | O(2^N) Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset wi
6 min read