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".

Visualization

Tap to expand
🎯 Factorial Division: Direct Permutation AccessPermutations starting with '1': positions 1-2123, 1322! = 2 permutationsPermutations starting with '2': positions 3-4213, 2312! = 2 permutationsPermutations starting with '3': positions 5-6312, 3212! = 2 permutationsFinding 4th Permutation (k=4)1. k=3 (0-indexed), (n-1)!=22. 3 ÷ 2 = 1 → start with digit[1]='2'3. k = 3 % 2 = 1, remaining: [1,3]4. 1 ÷ 1 = 1 → next digit[1]='3'Result: "231"🚀 Jump directly to any permutation without computing the rest!Time: O(n²) vs Brute Force: O(k×n) where k can be up to n!
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.

Time & Space Complexity

Time Complexity
⏱️
O(n²)

We iterate through n positions, and for each position we may need O(n) time to remove elements from the list

n
2n
Quadratic Growth
Space Complexity
O(n)

Space for storing the available digits list and result string

n
2n
Linearithmic Space

Constraints

  • 1 ≤ n ≤ 9
  • 1 ≤ k ≤ n!
  • k is guaranteed to be valid (within the range of possible permutations)
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
52.0K Views
Medium-High Frequency
~25 min Avg. Time
1.7K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen