Permutation Sequence
Hard Recursion
The digits 1 to n can be written in n! different orders, each a string of n digits that uses every digit once.
List those strings from smallest to largest and number them from 1: the first has its digits in increasing order, and the last in decreasing order.
Given n and k, return the string at place k of that list.
Example 1
The six orders of 1, 2 and 3, from smallest to largest, are 123, 132, 213, 231, 312 and 321. The fifth is 312.
Example 2
Each first digit begins 6 of the 24 orders, so places 13 to 18 begin with 3: 3124, 3142, 3214, 3241, 3412 and 3421. Place 17 is the fifth of them.
Example 3
Constraints
1 ≤ n ≤ 91 ≤ k ≤ n!, the number of orders (9!is 362,880)
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundMore Recursion
- Count Good NumbersAI round · Medium
- Find Kth Bit in Nth Binary StringAI round · Medium
- Elimination GameAI round · Medium
- Pow(x, n)Medium
- Power of ThreeEasy