Skip to main content
Back to problems
Leetcode
Medium
Arrays
Combinatorics
Math
Google
Permutation Sequence

Find the k-th permutation of the numbers from 1 to n in lexicographic order.

Acceptance 100%
Problem Statement

Permutation Sequence

Given two integers n and k, consider all permutations of the numbers 1 through n sorted in lexicographic order. Return the k-th permutation in that ordering.

You should construct the permutation directly rather than generating every permutation.

Example

For n = 3, the permutations in order are:

  1. 123
  2. 132
  3. 213
  4. 231
  5. 312
  6. 321

So for k = 4, the answer is 231.

Input Format

Input

  • Two integers n and k.

Interpretation

  • The set of values is 1, 2, ..., n.
  • k is 1-indexed in lexicographic order.

Output Format

Output

  • Return the k-th permutation as a string of digits in lexicographic order.

Constraints

  • 1 <= n <= 9
  • 1 <= k <= n!
  • The answer is guaranteed to exist.
  • Use each number from 1 to n exactly once.
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 3, k = 4

Output

231

Explanation

The lexicographic ordering is 123, 132, 213, 231, 312, 321. The 4th permutation is 231.

Example 2

Input

n = 4, k = 9

Output

2314

Explanation

There are 6 permutations for each fixed first digit. The 9th permutation falls in the block starting with 2, then the remaining choices lead to 2314.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.