VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-k-th-permutation-sequence-of-first-n-natural-numbers/

⇱ K-th Permutation of first N Natural Numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

K-th Permutation of first N Natural Numbers

Last Updated : 20 May, 2026

Given two integers n and k, find the k-th permutation sequence of the first n natural numbers arranged in lexicographical order. Return the answer as a string.

Examples:

Input: n = 4, k = 3
Output: 1324
Explanation: The permutations in lexicographical order are 1234, 1243, 1324, ..., so the 3rd permutation is 1324.

Input: n = 3, k = 5
Output: 312
Explanation: The permutations in lexicographical order are 123, 132, 213, 231, 312, 321, so the 5th permutation is 312.

[Naive Approach] Generate All Permutations - O(n! × n) Time and O(n!) Space

The idea is to generate all possible permutations of numbers. Store all permutations in a list, sort them lexicographically and return the k-th permutation.

  • Create a string containing numbers from 1 to n.
  • Generate all permutations recursively and store every permutation in a list.
  • Sort the list lexicographically and return the (k - 1) index permutation.

Output
312

[Expected Approach] Factorial Number System - O(n²) Time and O(n) Space

The idea is based on the observation that in all permutations of n numbers, each number appears in the first position exactly (n - 1)! times. Hence, the first digit of the k-th permutation can be directly determined using: index = k / (n - 1)!.

Once the digit is selected, it is removed from the list of available digits since it cannot be reused. The problem then reduces to finding the permutation of the remaining n - 1 digits.

Next, update k as k = k % (n - 1)! to focus only on the remaining block of permutations. This process is repeated for each position until all digits are selected.

  • Precompute factorials in an array fact[].
  • Create a nums[] array to track used digits, where 0 indicates unused and 1 indicates used.
  • Convert k into 0-based indexing by doing k = k - 1.
  • Compute the size of one permutation block using fact[n - 1].
  • Find the position using c = k / fact[n - 1] + 1, which determines the next unused digit to be selected.
  • Traverse all digits and count only those that are unused.
  • When the count reaches c, select that digit, mark it as used, and append it to the result.
  • Update k = k % fact[n - 1].
  • Repeat the same process for the remaining positions until all digits are selected.

Consider n = 3 and k = 5, initially available numbers are [1, 2, 3], after converting k into 0- based indexing k = 4, and the factorial values are 0! = 1, 1! = 1, and 2! = 2.

Step 1: Find the First Digit

  • Remaining numbers: [1, 2, 3], block size: (3 - 1)! = 2! = 2
  • Find index using: index = 4 / 2 = 2 . So, digit at index 2 is 3.
  • Answer becomes "3", remove 3 from available numbers -> [1, 2], and update: k = 4 % 2 = 0

Step 2: Find the Second Digit

  • Remaining numbers: [1, 2], block size: (2 - 1)! = 1! = 1
  • Find index using: index = 0 / 1 = 0. So, digit at index 0 is 1.
  • Answer becomes "31", remove 1 from available numbers -> [2], and update: k = 0 % 1 = 0

Step 3: Find the Last Digit

  • Only one number is left: [2]
  • Add 2 to the answer

Final answer becomes: "312"


Output
312
Comment
Article Tags: