Iterating over all possible combinations in an Array using Bits
There arise several situations while solving a problem where we need to iterate over all possible combinations of an array. In this article, we will discuss the method of using bits to do so.
For the purpose of explaining, consider the following question:
Given an array b[] = {2, 1, 4}. The task is to check if there exists any combination of elements of this array whose sum of elements is equal to k = 6.
Solution using Bit operations:
As there are 3 elements in this array, hence we need 3 bits to represent each of the numbers. A bit set as 1 corresponding to the element means it is included while calculating the sum, and not if it is 0.
The possible combinations are:
000 : No element is selected.
001 : 4 is selected.
010 : 1 is selected.
011 : 1 and 4 are selected.
100 : 2 is selected.
101 : 2 and 4 are selected.
110 : 2 and 1 are selected.
111 : All elements are selected.
Hence, the range required to access all these bits is 0 – 7. We iterate over each bit of each of the possible combinations, and check for each combination if the sum of chosen elements is equal to the required sum or not.
Examples:
Input : A = {3, 4, 1, 2} and k = 6
Output : YES
Here, the combination of using 3, 1 and 2 yields
the required sum.
Input : A = {3, 4, 1, 2} and k = 11
Output : NO
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkSum( int a[], int n, int k)
{
int flag = 0;
int range = (1 << n) - 1;
for ( int i = 0; i <= range; i++) {
int x = 0, y = i, sum = 0;
while (y > 0) {
if (y & 1 == 1) {
sum = sum + a[x];
}
x++;
y = y >> 1;
}
if (sum == k)
return true ;
}
return false ;
}
int main()
{
int k = 6;
int a[] = { 3, 4, 1, 2 };
int n = sizeof (a)/ sizeof (a[0]);
if (checkSum(a, n, k))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean checkSum( int a[], int n, int k)
{
int flag = 0 ;
int range = ( 1 << n) - 1 ;
for ( int i = 0 ; i <= range; i++)
{
int x = 0 , y = i, sum = 0 ;
while (y > 0 )
{
if ((y & 1 ) == 1 )
{
sum = sum + a[x];
}
x++;
y = y >> 1 ;
}
if (sum == k)
return true ;
}
return false ;
}
public static void main(String[] args)
{
int k = 6 ;
int a[] = { 3 , 4 , 1 , 2 };
int n = a.length;
if (checkSum(a, n, k))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkSum(a, n, k):
flag = 0
range__ = ( 1 << n) - 1
for i in range (range__ + 1 ):
x = 0
y = i
sum = 0
while (y > 0 ):
if (y & 1 = = 1 ):
sum = sum + a[x]
x + = 1
y = y >> 1
if ( sum = = k):
return True
return False
if __name__ = = '__main__' :
k = 6
a = [ 3 , 4 , 1 , 2 ]
n = len (a)
if (checkSum(a, n, k)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool checkSum( int [] a, int n, int k)
{
int
using System;
class GFG
{
static bool checkSum( int [] a, int n, int k)
{
int flag = 0;
int range = (1 << n) - 1;
for ( int i = 0; i <= range; i++)
{
int x = 0, y = i, sum = 0;
while (y > 0)
{
if ((y & 1) == 1)
{
sum = sum + a[x];
}
x++;
y = y >> 1;
}
if (sum == k)
return true ;
}
return false ;
}
public static void Main()
{
int k = 6;
int [] a = { 3, 4, 1, 2 };
int n = a.Length;
if (checkSum(a, n, k))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function checkSum( $a , $n , $k )
{
$flag = 0;
$range = (1 << $n ) - 1;
for ( $i = 0; $i <= $range ; $i ++)
{
$x = 0;
$y = $i ;
$sum = 0;
while ( $y > 0)
{
if ( $y & 1 == 1)
{
$sum = $sum + $a [ $x ];
}
$x ++;
$y = $y >> 1;
}
if ( $sum == $k )
return true;
}
return false;
}
$k = 6;
$a = array ( 3, 4, 1, 2 );
$n = sizeof( $a );
if (checkSum( $a , $n , $k ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function checkSum(a, n, k)
{
let flag = 0;
let range = (1 << n) - 1;
for (let i = 0; i <= range; i++)
{
let x = 0, y = i, sum = 0;
while (y > 0)
{
if ((y & 1) == 1)
{
sum = sum + a[x];
}
x++;
y = y >> 1;
}
if (sum == k)
return true ;
}
return false ;
}
let k = 6;
let a = [ 3, 4, 1, 2 ];
let n = a.length;
if (checkSum(a, n, k))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity : 2(number of bits)
Last Updated :
03 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...