1147. Every Ordering
Given n distinct integers, print every ordering of them, one per line, in
lexicographic order.
Problem #227 counts the orderings, which is n! and needs no search at all.
Producing them is the different job.
Unlike subsets, where each value is simply in or out, here every unused value is a branch. So the search needs to know which values are already placed, and the undo has to release a value as well as remove it from the current arrangement.
Sorting first and walking the values in order gives lexicographic output with no sorting at the end.
Constraints - `1 ≤ n ≤ 8` - `-1000 ≤ a[i] ≤ 1000` - All values are distinct.
Input
The first line contains an integer n.
The second line contains n space-separated distinct integers.
Output
Print every ordering on its own line, values separated by single spaces, the lines in lexicographic order.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.