Count of numbers which can be made power of 2 by given operation | C ++ implementation of the approach ; Function that returns true if x is a power of 2 ; If x & ( x - 1 ) = 0 then x is a power of 2 ; Function to return the required count ; If a [ i ] or ( a [ i ] + 1 ) is a power of 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfTwo ( int x ) { if ( x == 0 ) return false ; if ( ! ( x & ( x - 1 ) ) ) return true ; else return false ; } int countNum ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerOfTwo ( a [ i ] ) || isPowerOfTwo ( a [ i ] + 1 ) ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 5 , 6 , 9 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countNum ( arr , n ) ; return 0 ; }