Generate All Permutations
Mediumjavapythonccppjavascript
Given an array of distinct integers, generate every ordering of its elements, in lexicographic order.
Input Format
The first line contains an integer N. The second line contains N space-separated distinct integers.
Output Format
Print N! lines, one per permutation, in lexicographic order (based on the values), space-separated.
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
Explanation: There are 3! = 6 arrangements, listed in lexicographic order.
- 1 <= N <= 8
- -1000000000 <= A[i] <= 1000000000