SDE Path

Generate All Permutations of an Array

Medium

Generate All Permutations of an Array

Generate all possible permutations of a given array of distinct integers.

Input format

The first line contains N (N ≤ 6). The second line contains N distinct integers.

Output format

Print every permutation on its own line as space-separated values. Sort the lines lexicographically as strings.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
3
1 2 3
Expected output
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Example 2
Input
1
7
Expected output
7