Find the smallest integer whose binary representation contains exactly the same number of set bits as the given number.
Given a positive integer , return the smallest positive integer such that has the same number of set bits (1s) in its binary representation as .
In other words, if contains set bits, your task is to find the minimum possible value of an integer with exactly set bits.
Input Format
- A single positive integer .
Output Format
- Return the smallest positive integer that has the same number of set bits as .
Constraints
- The answer fits in the usual integer range for the platform.
Example 1
Input
n = 5
Output
3
Explanation
5 in binary is 101, which has 2 set bits. The smallest positive integer with 2 set bits is 11 in binary, which is 3.
Example 2
Input
n = 12
Output
3
Explanation
12 in binary is 1100, which has 2 set bits. The smallest number with 2 set bits is 3 (11 in binary).
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.