Skip to the code
cf.
+50 XP

#300 K-th Onboarding Order

hard

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.

Example 1

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

Example 2

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.

Constraints

  • 1 <= n <= 9
  • 1 <= k <= n!
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).