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

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate