K-th Smallest in Lexicographical Order

Try to solve the K-th Smallest in Lexicographical Order problem.

Statement

Given two integers, n and k, return the kthk^{th} smallest number in the range [1, n] when the numbers are sorted lexicographically.

Note: Lexicographical sorting means ordering numbers like words in a dictionary (alphabetical order)—digit by digit from left to right. For example, the numerical order of 1, 5, and 10 is [1, 5, 10], but their lexicographical order is [1, 10, 5].

Constraints:

  • 11 \leq k \leq n 109\leq 10^9

Examples

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.