The set [1, 2, ..., n] has n! permutations. Listing them in order gives a sequence; given n and k, return the k-th permutation sequence (1-indexed) as a string.
We use analytics and advertising cookies to understand how the site is used and whether our ads on Facebook and Instagram work. They are set only if you accept. See our Privacy Policy for details.
The set [1, 2, ..., n] has n! permutations. Listing them in order gives a sequence; given n and k, return the k-th permutation sequence (1-indexed) as a string.
A hard math problem, graded against 9 test cases (7 of them hidden).
Number theory, digit manipulation, and closed-form reasoning.
Reach for it when you see: Primes, digits, base conversion, or a problem with a formula hiding behind it.
More Math problems →Factorial number system. Precompute factorials. With `k` made 0-indexed, the first digit's position is `k / (n-1)!`; remove it, take `k % (n-1)!`, and repeat for the rest. This builds the answer in O(n²) without listing permutations.
Time: O(n²). Space: O(n).
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
These apply to the pattern as a whole, not just this problem.