K-th Permutation Sequence of first N natural numbers
You’re given two integers N and K, your task is to find the Kth permutation of the sequence of first N natural numbers.
For Example :
If N = 3 and K = 3.
The Possible Permutations for N = 3 are {(1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), (3,2,1)}.
In this list, the 3rd Permutation sequence is 2 1 3.
Input Format :
The first line of the input contains an integer T denoting the number of test cases. The 'T' test cases follow.
The first line of each test case contains two space-separated integers N and K, as specified in the problem statement.
Output Format :
For each test case, print an array denoting the Kth permutation of the sequence of first N natural numbers.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 5 * 10^3
1 <= K <= min(N!, 5 * 10^3)
Time limit: 1 sec
AnswerBot
1y
The task is to find the Kth permutation of the sequence of first N natural numbers.
Generate all possible permutations of the sequence of first N natural numbers
Sort the permutations in lexicographical...read more
CodingNinjas
author
2y
Insert all natural numbers upto n in a set.
Use iterator to traverse the set.
First position of the kth sequence will be occupied by the number present at index = k / (n-1)!
For each value in the set, fi...read more
CodingNinjas
author
2y
Recursive Approach
- We’ll make a function named ‘kthPermutation’ with two integer parameters n and k.
- Inside this function, we’ll make a 2 Dimensional array named ‘answer’ and an array named ‘v’ and then...read more
CodingNinjas
author
2y
Mathematics Based Approach
- We start by making an array named ‘answer’, which will store the Kth permutation.
- Then we’ll make an array named ‘numbersLeft’ and we’ll store all values from 1 till N inside ...read more
Add answer anonymously...
Top HyperVerge Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
>
HyperVerge Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app