Smallest Number With All Set Bits

You are given a positive number n.

Return the smallest number x greater than or equal to n, such that the binary representation of x contains only set bits.

Example 1
Inputn = 5
Output7
The binary representation of 7 is "111".
Example 2
Inputn = 10
Output15
The binary representation of 15 is "1111".

Constraints

  • 1 <= n <= 1000

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate