Check if bitwise AND of any subset is power of two
Last Updated :
10 Aug, 2022
Given an array arr[] of n positive integers. The task is to check if there exist any subset of the array whose bitwise AND is a power of two (i.e 1, 2, 4, 8, 16, …).
Note : There might be exist two or more subset of given array whose bitwise AND becomes power of two. Return YES if there exist at least one subset otherwise return NO.
Examples:
Input : n = 3, arr[] = { 12, 13, 7 }
Output : Yes
Explanation : Subset {12, 13, 7} and { 12, 7 } have Bitwise AND value 4 and 4 resepectively, which
are power of 2.
Input : n = 2, arr[] = { 10, 20 }
Output : No
Observe, for a number to be the power of 2, it should have only 1 set bit.
If n is 1, then we simply check if the number has the only single set bit.
For n is greater than one, our task becomes to choose those numbers from the array whose bitwise AND leads to an only single bit set number. To do so, we search a position, at which all elements in the set has a bit set at that position. For example, for set { 4 (100), 6 (110), 7 (111) }, at position 2 (from right to left, 0-based indexing) bit is set for all element. So, doing bitwise AND gives 4, which is a power of 2.
Below is the implementation of this approach:
Illustration :
12 –> 01100
13 –> 01101
7 –> 00111
For position = 0(from right to left, 0-based indexing), there exist 13 and 17 whose bit is setbit.
total –> 1111111111111111111111
13 –> 0000000000000000001101
7 –> 0000000000000000000111
—————————–
bitwise AND –> 0000000000000000000101
bitwise AND is not power of 2 so it is not a valid subset.
For position = 1, there exist only 7 whose bit is setbit.
total –> 1111111111111111111111
7 –> 0000000000000000000111
——————————
bitwise AND –> 0000000000000000000111
bitwise AND is not power of 2 so it is not a valid subset.
For position = 2, there exist 12, 13 and 7 whose bit is setbit.
total –> 1111111111111111111111
12 –> 0000000000000000001100
13 –> 0000000000000000001101
7 –> 0000000000000000000111
——————————
bitwise AND –> 0000000000000000000100
bitwise AND is power of 2 so it is a valid subset.
Similarly, we can check for remaining positions.
Algorithm :
- If size of array is 1 then simply check whether first element of array is power of 2 or not.
- Otherwise, create a variable total = 0 and make its all bits to setbit.
- Traverse from i=0 to i = 31 for every bit.
- Create a variable ans = total.
- Traverse in an array and if the bit of current element at position i is setbit then change ans variable to bitwise AND of ans and current element.
- After completion of traversal in array. Check our ans is power of two or not.
- If ans is power of two then return true. Otherwise, traverse for next bit.
- After traversing every bit if we did not found any subset then return false.
Implementation :
C++
#include <bits/stdc++.h>
using namespace std;
const int NUM_BITS = 32;
bool isPowerOf2( int num)
{
return (num && !(num & (num - 1)));
}
bool checkSubsequence( int arr[], int n)
{
if (n == 1)
return isPowerOf2(arr[0]);
int total = 0;
for ( int i = 0; i < NUM_BITS; i++)
total = total | (1 << i);
for ( int i = 0; i < NUM_BITS; i++) {
int ans = total;
for ( int j = 0; j < n; j++) {
if (arr[j] & (1 << i))
ans = ans & arr[j];
}
if (isPowerOf2(ans))
return true ;
}
return false ;
}
int main()
{
int arr[] = { 12, 13, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
if (checkSubsequence(arr, n))
printf ( "YES\n" );
else
printf ( "NO\n" );
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static int NUM_BITS = 32 ;
static boolean isPowerOf2( int num)
{
if (num != 0 && (num & (num - 1 )) == 0 )
return true ;
return false ;
}
static boolean checkSubsequence( int []arr, int n)
{
if (n == 1 )
return isPowerOf2(arr[ 0 ]);
int total = 0 ;
for ( int i = 0 ; i < NUM_BITS; i++)
total = total | ( 1 << i);
for ( int i = 0 ; i < NUM_BITS; i++)
{
int ans = total;
for ( int j = 0 ; j < n; j++)
{
int p = arr[j] & ( 1 << i);
if (p == 0 )
ans = ans & arr[j];
}
if (isPowerOf2(ans))
return true ;
}
return false ;
}
public static void main(String args[])
{
int []arr = { 12 , 13 , 7 };
int n = arr.length;
if (checkSubsequence(arr, n))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
NUM_BITS = 32
def isPowerOf2(num):
return (num and (num & (num - 1 )) = = 0 )
def checkSubsequence(arr, n):
if (n = = 1 ):
return isPowerOf2(arr[ 0 ])
total = 0
for i in range ( 0 , NUM_BITS):
total = total | ( 1 << i)
for i in range ( 0 , NUM_BITS):
ans = total
for j in range ( 0 , n):
if (arr[j] & ( 1 << i)):
ans = ans & arr[j]
if (isPowerOf2(ans)):
return True
return False
arr = [ 12 , 13 , 7 ]
n = len (arr)
if (checkSubsequence(arr, n)):
print ( "YES\n" )
else :
print ( "NO\n" )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int NUM_BITS = 32;
static bool isPowerOf2( int num)
{
if (num != 0 && (num & (num - 1)) == 0)
return true ;
return false ;
}
static bool checkSubsequence( int []arr, int n)
{
if (n == 1)
return isPowerOf2(arr[0]);
int total = 0;
for ( int i = 0; i < NUM_BITS; i++)
total = total | (1 << i);
for ( int i = 0; i < NUM_BITS; i++)
{
int ans = total;
for ( int j = 0; j < n; j++)
{
int p = arr[j] & (1 << i);
if (p == 0)
ans = ans & arr[j];
}
if (isPowerOf2(ans))
return true ;
}
return false ;
}
public static void Main()
{
int []arr = {12, 13, 7};
int n = arr.Length;
if (checkSubsequence(arr, n))
Console.Write( "YES\n" );
else
Console.Write( "NO\n" );
}
}
|
PHP
<?php
function isPowerOf2( $num )
{
return ( $num && !( $num & ( $num - 1)));
}
function checkSubsequence( $arr , $n )
{
$NUM_BITS = 32;
if ( $n == 1)
return isPowerOf2( $arr [0]);
$total = 0;
for ( $i = 0; $i < $NUM_BITS ; $i ++)
$total = $total | (1 << $i );
for ( $i = 0; $i < $NUM_BITS ; $i ++)
{
$ans = $total ;
for ( $j = 0; $j < $n ; $j ++)
{
if ( $arr [ $j ] & (1 << $i ))
$ans = $ans & $arr [ $j ];
}
if (isPowerOf2( $ans ))
return true;
}
return false;
}
$arr = array (12, 13, 7);
$n = sizeof( $arr ) / sizeof( $arr [0]);
if (checkSubsequence( $arr , $n ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
var NUM_BITS = 32;
function isPowerOf2(num)
{
return (num && !(num & (num - 1)));
}
function checkSubsequence(arr, n)
{
if (n == 1)
return isPowerOf2(arr[0]);
var total = 0;
for ( var i = 0; i < NUM_BITS; i++)
total = total | (1 << i);
for ( var i = 0; i < NUM_BITS; i++) {
var ans = total;
for ( var j = 0; j < n; j++) {
if (arr[j] & (1 << i))
ans = ans & arr[j];
}
if (isPowerOf2(ans))
return true ;
}
return false ;
}
var arr = [ 12, 13, 7 ];
var n = arr.length;
if (checkSubsequence(arr, n))
document.write( "YES<br>" );
else
document.write( "NO<br>" );
</script>
|
Time Complexity : O(N), [(32)* (length of array) where 32 is constant time, so as per recurrence tree the time complexity is of N order]
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...