Same Number Of Set Bits As N | returns number of set bits in a number ; function ; __builtin_popcount function that count set bits in n ; Iterate from n - 1 to 1 ; check if the number of set bits equals to temp increment count ; Driver Code
< ? php function __builtin_popcount ( $ n ) { $ t = 0 ; while ( $ n > 0 ) { $ d = $ n % 2 ; $ n = intval ( $ n / 2 ) ; if ( $ d == 1 ) $ t ++ ; } return $ t ; } function smallerNumsWithSameSetBits ( $ n ) { $ temp = __builtin_popcount ( $ n ) ; $ count = 0 ; for ( $ i = $ n - 1 ; $ i > 0 ; $ i -- ) { if ( $ temp == __builtin_popcount ( $ i ) ) $ count ++ ; } return $ count ; } $ n = 4 ; echo ( smallerNumsWithSameSetBits ( $ n ) ) ; ? >