#C6512. Generate r-length Permutations

    ID: 50281 Type: Default 1000ms 256MiB

Generate r-length Permutations

Generate r-length Permutations

Given a list of distinct elements and an integer (r), your task is to generate all possible permutations of the list taken (r) at a time. Recall that the total number of such permutations is given by (P(n, r)=\frac{n!}{(n-r)!}) where (n) is the number of elements in the list.

For example, if the input list is [A, B, C] and (r=2), the expected output (one permutation per line) is:
A B
A C
B A
B C
C A
C B

Implement your solution so that it reads from standard input (stdin) and writes to standard output (stdout).

inputFormat

The input is given via stdin and consists of three parts:
1. An integer (n), the number of elements in the list.
2. A line containing (n) space-separated elements.
3. An integer (r), the length of each permutation.

For example:
3
A B C
2

outputFormat

Output all (r)-length permutations of the given list, one permutation per line. Each permutation should display the chosen elements separated by a single space, in the order they are generated.## sample

3
A B C
2
A B

A C B A B C C A C B

</p>