Count of numbers which can be made power of 2 by given operation | 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
< ? php function isPowerOfTwo ( $ x ) { if ( $ x == 0 ) return false ; if ( ! ( $ x & ( $ x - 1 ) ) ) return true ; else return false ; } function countNum ( $ a , $ n ) { $ cnt = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( isPowerOfTwo ( $ a [ $ i ] ) || isPowerOfTwo ( $ a [ $ i ] + 1 ) ) $ cnt ++ ; } return $ cnt ; } $ arr = array ( 5 , 6 , 9 , 3 , 1 ) ; $ n = count ( $ arr ) ; echo countNum ( $ arr , $ n ) ; ? >