#K96272. Nth Permutation Finder

    ID: 39050 Type: Default 1000ms 256MiB

Nth Permutation Finder

Nth Permutation Finder

You are given a list of distinct elements and a positive integer n. Your task is to determine the n-th permutation (in lexicographic order, 1-indexed) of these elements. If n is greater than the total number of possible permutations of the list, output "Invalid Input".

The input begins with an integer T representing the number of test cases. Each test case consists of three lines:

  • An integer N which is the number of elements.
  • A line containing N space-separated characters or strings representing the elements.
  • An integer n representing the permutation number to find.

For each test case, output the n-th permutation as a space separated string. If the permutation does not exist, output "Invalid Input".

Note: The elements should be considered in lexicographic order when forming permutations.

inputFormat

The first line of input contains a single integer T, the number of test cases. For each test case, there are three subsequent lines:

  • An integer N, the number of elements.
  • A line with N space-separated elements (each element can be a string or a character).
  • An integer n which is the permutation index (1-indexed) to find.

outputFormat

For each test case, output the n-th permutation in lexicographic order as a single line of space-separated elements. If n is larger than the number of available permutations, output "Invalid Input".

## sample
4
3
a b c
1
3
a b c
4
3
1 2 3
7
4
a b c d
24
a b c

b c a Invalid Input d c b a

</p>