Search⌘ K
AI Features

Find kth Permutation

Explore how to determine the kth permutation of a sequence of n elements by understanding the factorial concept and applying a recursive strategy to select elements in blocks. This lesson guides you to optimize the search for permutations beyond naive generation, helping you implement a linear time solution with clear mathematical reasoning.

Statement

Given two numbers nn and kk, find kth^{th} permutation of nn. For n=3n = 3, all permutations are (with ordering):

g array 1st 123 2nd 132 3rd 213 4th 231 5th 312 6th 321

Sample input

Here 4 is the number nn and 8 is the k ...