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
#include <bits/stdc++.h>
using namespace std;
#define BITS 32
int andSum( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < BITS; i++) {
int countSetBits = 0;
for ( int j = 0; j < n; j++) {
if (arr[j] & (1 << i))
countSetBits++;
}
int subset = (1 << countSetBits) - 1;
subset = (subset * (1 << i));
ans += subset;
}
return ans;
}
int main()
{
int arr[] = { 1, 2, 3};
int size = sizeof (arr) / sizeof (arr[0]);
cout << andSum(arr, size);
return 0;
}
|
Java
class GFG {
static final int BITS = 32 ;
static int andSum( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < BITS; i++) {
int countSetBits = 0 ;
for ( int j = 0 ; j < n; j++) {
if ((arr[j] & ( 1 << i)) != 0 )
countSetBits++;
}
int subset = ( 1 << countSetBits) - 1 ;
subset = (subset * ( 1 << i));
ans += subset;
}
return ans;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 };
int size = 3 ;
System.out.println (andSum(arr, size));
}
}
|
Python3
BITS = 32 ;
def andSum(arr, n):
ans = 0
for i in range ( 0 , BITS):
countSetBits = 0
for j in range ( 0 , n) :
if (arr[j] & ( 1 << i)) :
countSetBits = (countSetBits
+ 1 )
subset = (( 1 << countSetBits)
- 1 )
subset = (subset * ( 1 << i))
ans = ans + subset
return ans
arr = [ 1 , 2 , 3 ]
size = len (arr)
print (andSum(arr, size))
|
C#
using System;
class GFG {
static int BITS = 32;
static int andSum( int [] arr, int n)
{
int ans = 0;
for ( int i = 0; i < BITS; i++) {
int countSetBits = 0;
for ( int j = 0; j < n; j++) {
if ((arr[j] & (1 << i)) != 0)
countSetBits++;
}
int subset = (1 << countSetBits) - 1;
subset = (subset * (1 << i));
ans += subset;
}
return ans;
}
static public void Main()
{
int []arr = { 1, 2, 3};
int size = 3;
Console.WriteLine (andSum(arr, size));
}
}
|
PHP
<?php
$BITS = 32;
function andSum( $arr , $n )
{
global $BITS ;
$ans = 0;
for ( $i = 0; $i < $BITS ; $i ++)
{
$countSetBits = 0;
for ( $j = 0; $j < $n ; $j ++) {
if ( $arr [ $j ] & (1 << $i ))
$countSetBits ++;
}
$subset = (1 << $countSetBits ) - 1;
$subset = ( $subset * (1 << $i ));
$ans += $subset ;
}
return $ans ;
}
$arr = array (1, 2, 3);
$size = count ( $arr );
echo andSum( $arr , $size );
?>
|
Javascript
<script>
var BITS = 32;
function andSum(arr , n) {
var ans = 0;
for (i = 0; i < BITS; i++) {
var countSetBits = 0;
for (j = 0; j < n; j++) {
if ((arr[j] & (1 << i)) != 0)
countSetBits++;
}
var subset = (1 << countSetBits) - 1;
subset = (subset * (1 << i));
ans += subset;
}
return ans;
}
var arr = [ 1, 2, 3 ];
var size = 3;
document.write(andSum(arr, size));
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)