Permutation Sequence - Problem
The mathematical foundation of permutations tells us that a set of n distinct elements produces exactly n! unique permutations. When we arrange these permutations in lexicographical (dictionary) order, we create a predictable sequence.
For example, with n = 3, the set [1, 2, 3] generates these 6 permutations in order:
- 1st: "123"
- 2nd: "132"
- 3rd: "213"
- 4th: "231"
- 5th: "312"
- 6th: "321"
Your challenge: Given n and k, find the k-th permutation in this sequence without generating all previous permutations.
This problem tests your ability to think mathematically about combinatorics and avoid the computational explosion of factorial complexity.
Input & Output
example_1.py — Basic Case
$
Input:
n = 3, k = 3
›
Output:
"213"
💡 Note:
For n=3, the permutations in order are: 123, 132, 213, 231, 312, 321. The 3rd permutation is "213".
example_2.py — Larger Input
$
Input:
n = 4, k = 9
›
Output:
"2314"
💡 Note:
With n=4, there are 4!=24 permutations. The 9th permutation in lexicographical order is "2314".
example_3.py — Edge Case
$
Input:
n = 3, k = 1
›
Output:
"123"
💡 Note:
The 1st (first) permutation is always the natural ascending order: "123".
Constraints
- 1 ≤ n ≤ 9
- 1 ≤ k ≤ n!
- k is guaranteed to be valid (within the range of possible permutations)
Visualization
Tap to expand
Understanding the Visualization
1
Understand the Pattern
For n digits, exactly (n-1)! permutations start with each digit
2
Calculate Position
Use k÷(n-1)! to find which digit starts our target permutation
3
Remove and Recurse
Remove chosen digit and repeat for remaining positions
4
Build Result
Concatenate all chosen digits to form the k-th permutation
Key Takeaway
🎯 Key Insight: Mathematics beats brute force! By understanding that each digit 'reserves' exactly (n-1)! positions, we can calculate any permutation directly.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code