An onboarding flow has n steps numbered 1 to n. For an experiment, each user gets a different order of the steps. All n! orders are listed in sorted order and numbered from 1.
Write kthOrder(n, k) that returns the k-th order as an array, without listing all the orders before it.
With 13 steps there are over 6 billion orders, and k can be over 2 billion, so listing the orders one by one is far too slow.
Input: n = 3, k = 3 Output: [2,1,3]
The sorted orders start [1, 2, 3], [1, 3, 2], [2, 1, 3], so the third is [2, 1, 3].
Input: n = 4, k = 9 Output: [2,3,1,4]
Orders 1 to 6 start with 1, so order 9 is the third one that starts with 2.
1 <= n <= 131 <= k <= n!, and k <= 2,147,483,647Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #300 K-th Onboarding Order. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.