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 good permutation of first N natural numbers
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Find permutation of first N natural numbers that satisfies the given condition
- Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible
- Increasing permutation of first N natural numbers
- Number of valid indices in the permutation of first N natural numbers
- Minimum cost to make an Array a permutation of first N natural numbers
- Count array elements that can be maximized by adding any permutation of first N natural numbers
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Sort permutation of N natural numbers using triple cyclic right swaps
- Find m-th summation of first n natural numbers.
- Program to find sum of first n natural numbers
- Find the average of first N natural numbers
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Find all divisors of first N natural numbers
- Find if given number is sum of first n natural numbers
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Find the Kth position element of the given sequence
- Minimum number of given operations required to convert a permutation into an identity permutation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.