Find Nth Smallest Integer With K One Bits

You are given two positive integers n and k.

Return an integer denoting the n^th smallest positive integer that has exactly k ones in its binary representation. It is guaranteed that the answer is strictly less than 2^50.

Example 1
Inputn = 4, k = 2
Output9
The 4 smallest positive integers with exactly 2 ones in binary are 3, 5, 6, and 9, so the answer is 9.
Example 2
Inputn = 3, k = 1
Output4
The 3 smallest positive integers with exactly 1 one in binary are 1, 2, and 4, so the answer is 4.

Constraints

  • 1 <= n <= 2^50
  • 1 <= k <= 50
  • The answer is strictly less than 2^50.

Asked at 1 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