StaffTrie

K-th Smallest in Lexicographical Order

Given two integers n and k, return the k^th lexicographically smallest integer in the range [1, n].

Example 1
Inputn = 13, k = 2
Output10
The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.
Example 2
Inputn = 1, k = 1
Output1
The only integer in the range is 1, so the first lexicographically smallest integer is 1.

Constraints

  • 1 <= k <= n <= 10^9

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