Given two integers N and K, find the Kth permutation sequence of numbers from 1 to N without using STL function.
Note: Assume that the inputs are such that Kth permutation of N number is always possible.
Input: N = 3, K = 4
The ordered list of permutation sequence from integer 1 to 3 is : 123, 132, 213, 231, 312, 321. So, the 4th permutation sequence is “231”.
Input: N = 2, K = 1
For n = 2, only 2 permutations are possible 12 21. So, the 1st permutation sequence is “12”.
To solve the problem mentioned above the simple approach is to find all permutation sequences and output the kth out of them. But this method is not so efficient and takes more time, hence it can be optimized.
To optimize the above method mentioned above, observe that the value of k can be directly used to find the number at each index of the sequence.
- The first position of an n length sequence is occupied by each of the numbers from 1 to n exactly n! / n that is (n-1)! number of times and in ascending order. So the first position of the kth sequence will be occupied by the number present at index = k / (n-1)! (according to 1-based indexing).
- The currently found number can not occur again so it is removed from the original n numbers and now the problem reduces to finding the ( k % (n-1)! )th permutation sequence of the remaining n-1 numbers.
- This process can be repeated until we have only one number left which will be placed in the first position of the last 1-length sequence.
- The factorial values involved here can be very large as compared to k. So, the trick used to avoid the full computation of such large factorials is that as soon as the product n * (n-1) * … becomes greater than k, we no longer need to find the actual factorial value because:
k / n_actual_factorial_value = 0
and k / n_partial_factorial_value = 0
when partial_factorial_value > k
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible
- Find the good permutation of first N natural numbers
- Find permutation of first N natural numbers that satisfies the given condition
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Increasing permutation of first N natural numbers
- Number of valid indices in the permutation of first N natural numbers
- Sort permutation of N natural numbers using triple cyclic right swaps
- Minimum cost to make an Array a permutation of first N natural numbers
- Program to find sum of first n natural numbers
- Find sum of N-th group of Natural Numbers
- Find if given number is sum of first n natural numbers
- Find the average of first N natural numbers
- Find m-th summation of first n natural numbers.
- Find all divisors of first N natural numbers
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Find the count of natural Hexadecimal numbers of size N
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Find a sequence of N prime numbers whose sum is a composite number
- Count permutation such that sequence is non decreasing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.