Permutations

MediumRecursion & Backtracking
Next
BacktrackingRecursion

Given distinct integers, print every possible ordering (permutation) of them, one per line, in ascending lexicographic order.

Input

Line 1: n. Line 2: n distinct integers.

Output

n! lines, each a permutation with numbers separated by spaces, in lexicographic order.

Constraints

1 <= n <= 7 -10 <= each value <= 10

Example 1
Input
3
1 2 3
Output
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

The six orderings in lexicographic order.

Hints

Permutations - Medium Recursion & Backtracking Problem | Skill Shiksha