Same Number Of Set Bits As N |  ; 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
/ * returns number of set bits in a number * / NEW_LINE def __builtin_popcount ( n ) : NEW_LINE INDENT t = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT d = n % 2 NEW_LINE n = int ( n / 2 ) NEW_LINE if ( d == 1 ) : NEW_LINE INDENT t = t + 1 NEW_LINE DEDENT DEDENT return t NEW_LINE DEDENT def smallerNumsWithSameSetBits ( n ) : NEW_LINE INDENT temp = __builtin_popcount ( n ) NEW_LINE count = 0 NEW_LINE for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT if ( temp == __builtin_popcount ( i ) ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT n = 4 NEW_LINE print ( smallerNumsWithSameSetBits ( n ) ) NEW_LINE