Problem
Given n
and k
, return the k
-th permutation sequence of permutations of numbers {1, 2, .., n}
.
OR
The set [1, 2, 3, ..., n]
contains a total of n!
unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3
:
|
|
Given n
and k
, return the kth
permutation sequence.
Examples
By listing and labeling all of the permutations in order, we get the following sequence (ie, for n = 3):
|
|
For example, given n = 3, k = 4, ans = “231”
More Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Constraints
1 <= n <= 9
1 <= k <= n!
Notes
Good questions to ask the interviewer:
- What if n is greater than 10. How should multiple digit numbers be represented in string? –> In this case, just concatenate the number to the answer. So if n = 11, k = 1, ans = “1234567891011”
- Whats the maximum value of n and k? –> In this case, k will be a positive integer thats less than INT_MAX. n is reasonable enough to make sure the answer does not bloat up a lot.
Solution
Method 1 – Naive (Repeated Next Permutation)
Intuition
The simplest way to find the k-th permutation is to start with the first permutation (sorted order) and repeatedly generate the next permutation k-1 times. This uses the standard next-permutation algorithm.
Approach
- Start with the sequence
[1, 2, ..., n]
. - Apply the next permutation operation k-1 times.
- Return the resulting sequence as the k-th permutation.
This approach is straightforward but inefficient for large n or k.
Complexity
- ⏰ Time complexity:
O(n * k)
(since each next permutation is O(n), and we do it k-1 times; for k = n!, this is O(n * n!)). - 🧺 Space complexity:
O(n)
(for storing the current permutation).
Method 2 – Mathematical Construction
Intuition
I have discussed a similar problem of finding the next permutation sequence of a given permutation here - Next permutation of a number. We have also discussed - Next even permutation of a number.
Before reading this article further I strongly suggest to read my previous post to find Permutation Rank. Basically given a permutation we compute how many permutations is possible to fix a number in a particular position of the array. Then we sum total such permutations for all positions to get the rank of the permutation. Now, in this question we are asking for the reverse question i.e. given the rank, find the permutation.
Instead of generating all possible permutations, we use factorial math to directly determine the k-th permutation. The main insight is that for n numbers, the first digit determines blocks of (n-1)! permutations. By dividing k by these blocks, we can select the correct digit for each position.
Let’s break down the process with a detailed example:
Suppose n = 4 and k = 8. The full list of permutations for [1, 2, 3, 4] in lexicographic order is:
|
|
Step-by-step reasoning:
- There are 4! = 24 total permutations. For the first digit, each number fixes (4-1)! = 6 permutations. So, the first 6 permutations start with 1, the next 6 with 2, and so on.
- Since k = 8, and we use zero-based indexing (k-1 = 7), the first digit is at index 7/6 = 1, which is 2. So, our answer starts with 2.
- Remove 2 from the list. Now, the remaining numbers are
[1, 3, 4]
, andk' = 7 % 6 = 1
. - For the second digit, each number fixes (3-1)! = 2 permutations. The index is 1/2 = 0, so we pick 1. Our answer is now
[2, 1]
. - Remove 1. Remaining numbers:
[3, 4]
, k’’ = 1 % 2 = 1. - For the third digit, each number fixes (2-1)! = 1 permutation. The index is 1/1 = 1, so we pick 4. Our answer is
[2, 1, 4]
. - Remove 4. Remaining number:
[3]
, k’’’ = 1 % 1 = 0. - The last digit is 3. Final answer:
[2, 1, 4, 3]
.
This process generalizes for any n and k, allowing us to build the k-th permutation efficiently by selecting digits one by one using division and modulo operations with factorials.
Approach
- Start with a list of numbers from 1 to n.
- Precompute factorials up to n to help determine the number of permutations for each position.
- Adjust k to be zero-based (subtract 1).
- For each position:
- Find the index of the number to use by dividing k by the factorial of the remaining positions.
- Append the selected number to the result and remove it from the list.
- Update k to reflect the remaining permutations.
- Return the constructed permutation as a string.
Examples
Suppose n = 4, k = 8. The list of all permutations (lexicographically) is:
|
|
To find the 8th permutation:
- The first digit is the second number (since 7/6 = 1, zero-based), so start with 2.
- Remove 2, update k, and repeat for the next positions.
- Continue until all positions are filled.
- The result is
[2, 1, 4, 3]
.
Code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
— For each of n positions, we may need to remove an element from a list, which is O(n) in the worst case. - 🧺 Space complexity:
O(n)
— We use arrays/lists to store the numbers and factorials, both of size n.