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++
// CPP Program to check if Bitwise AND of any // subset is power of two #include <bits/stdc++.h> using namespace std; const int NUM_BITS = 32; // Check for power of 2 or not bool isPowerOf2( int num) { return (num && !(num & (num - 1))); } // Check if there exist a subset whose bitwise AND // is power of 2. bool checkSubsequence( int arr[], int n) { // if there is only one element in the set. if (n == 1) return isPowerOf2(arr[0]); // Finding a number with all bit sets. int total = 0; for ( int i = 0; i < NUM_BITS; i++) total = total | (1 << i); // check all the positions at which the bit is set. for ( int i = 0; i < NUM_BITS; i++) { int ans = total; for ( int j = 0; j < n; j++) { // include all those elements whose // i-th bit is set if (arr[j] & (1 << i)) ans = ans & arr[j]; } // check for the set contains elements // make a power of 2 or not if (isPowerOf2(ans)) return true ; } return false ; } // Driver Program 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
// Java Program to check if Bitwise AND of any // subset is power of two import java.io.*; import java.util.*; public class GFG { static int NUM_BITS = 32 ; // Check for power of 2 or not static boolean isPowerOf2( int num) { if (num != 0 && (num & (num - 1 )) == 0 ) return true ; return false ; } // Check if there exist a // subset whose bitwise AND // is power of 2. static boolean checkSubsequence( int []arr, int n) { // if there is only one // element in the set. if (n == 1 ) return isPowerOf2(arr[ 0 ]); // Finding a number with // all bit sets. int total = 0 ; for ( int i = 0 ; i < NUM_BITS; i++) total = total | ( 1 << i); // check all the positions // at which the bit is set. for ( int i = 0 ; i < NUM_BITS; i++) { int ans = total; for ( int j = 0 ; j < n; j++) { // include all those // elements whose // i-th bit is set int p = arr[j] & ( 1 << i); if (p == 0 ) ans = ans & arr[j]; } // check for the set // contains elements // make a power of 2 // or not if (isPowerOf2(ans)) return true ; } return false ; } // Driver Code 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" ); } } // This code is contributed by // Manish Shaw (manishshaw1) |
Python3
# Python3 Program to check if Bitwise AND of any # subset is power of two NUM_BITS = 32 # Check for power of 2 or not def isPowerOf2(num): return (num and (num & (num - 1 )) = = 0 ) # Check if there exist a subset whose bitwise AND # is power of 2. def checkSubsequence(arr, n): # if there is only one element in the set. if (n = = 1 ): return isPowerOf2(arr[ 0 ]) # Finding a number with all bit sets. total = 0 for i in range ( 0 , NUM_BITS): total = total | ( 1 << i) # check all the positions at which the bit is set. for i in range ( 0 , NUM_BITS): ans = total for j in range ( 0 , n): # include all those elements whose # i-th bit is set if (arr[j] & ( 1 << i)): ans = ans & arr[j] # check for the set contains elements # make a power of 2 or not if (isPowerOf2(ans)): return True return False # Driver Program arr = [ 12 , 13 , 7 ] n = len (arr) if (checkSubsequence(arr, n)): print ( "YES\n" ) else : print ( "NO\n" ) # This code is contributed by Manish Shaw # (manishshaw1) |
C#
// C# Program to check if Bitwise AND of any // subset is power of two using System; using System.Collections.Generic; class GFG { static int NUM_BITS = 32; // Check for power of 2 or not static bool isPowerOf2( int num) { if (num != 0 && (num & (num - 1)) == 0) return true ; return false ; } // Check if there exist a // subset whose bitwise AND // is power of 2. static bool checkSubsequence( int []arr, int n) { // if there is only one // element in the set. if (n == 1) return isPowerOf2(arr[0]); // Finding a number with // all bit sets. int total = 0; for ( int i = 0; i < NUM_BITS; i++) total = total | (1 << i); // check all the positions // at which the bit is set. for ( int i = 0; i < NUM_BITS; i++) { int ans = total; for ( int j = 0; j < n; j++) { // include all those // elements whose // i-th bit is set int p = arr[j] & (1 << i); if (p == 0) ans = ans & arr[j]; } // check for the set // contains elements // make a power of 2 // or not if (isPowerOf2(ans)) return true ; } return false ; } // Driver Code 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" ); } } // This code is contributed by // Manish Shaw (manishshaw1) |
PHP
<?php // PHP Program to check if // Bitwise AND of any subset // is power of two // Check for power of 2 or not function isPowerOf2( $num ) { return ( $num && !( $num & ( $num - 1))); } // Check if there exist a // subset whose bitwise AND // is power of 2. function checkSubsequence( $arr , $n ) { $NUM_BITS = 32; // if there is only one // element in the set. if ( $n == 1) return isPowerOf2( $arr [0]); // Finding a number with // all bit sets. $total = 0; for ( $i = 0; $i < $NUM_BITS ; $i ++) $total = $total | (1 << $i ); // check all the positions at // which the bit is set. for ( $i = 0; $i < $NUM_BITS ; $i ++) { $ans = $total ; for ( $j = 0; $j < $n ; $j ++) { // include all those // elements whose // i-th bit is set if ( $arr [ $j ] & (1 << $i )) $ans = $ans & $arr [ $j ]; } // check for the set // contains elements // make a power of 2 or not if (isPowerOf2( $ans )) return true; } return false; } // Driver Code $arr = array (12, 13, 7); $n = sizeof( $arr ) / sizeof( $arr [0]); if (checkSubsequence( $arr , $n )) echo "YES" ; else echo "NO" ; // This code is contributed by mits ?> |
Javascript
<script> // Javascript Program to check if Bitwise AND of any // subset is power of two var NUM_BITS = 32; // Check for power of 2 or not function isPowerOf2(num) { return (num && !(num & (num - 1))); } // Check if there exist a subset whose bitwise AND // is power of 2. function checkSubsequence(arr, n) { // if there is only one element in the set. if (n == 1) return isPowerOf2(arr[0]); // Finding a number with all bit sets. var total = 0; for ( var i = 0; i < NUM_BITS; i++) total = total | (1 << i); // check all the positions at which the bit is set. for ( var i = 0; i < NUM_BITS; i++) { var ans = total; for ( var j = 0; j < n; j++) { // include all those elements whose // i-th bit is set if (arr[j] & (1 << i)) ans = ans & arr[j]; } // check for the set contains elements // make a power of 2 or not if (isPowerOf2(ans)) return true ; } return false ; } // Driver Program var arr = [ 12, 13, 7 ]; var n = arr.length; if (checkSubsequence(arr, n)) document.write( "YES<br>" ); else document.write( "NO<br>" ); // This code is contributed by itsok. </script> |
YES
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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!