#K11491. Generate Lexicographically Sorted Permutations
Generate Lexicographically Sorted Permutations
Generate Lexicographically Sorted Permutations
Given a positive integer \( n \), generate all permutations of the numbers from 1 to \( n \) in lexicographic (dictionary) order. If \( n \) is not a valid positive integer (i.e., if \( n\le0 \) or \( n \) cannot be parsed as an integer), output an empty list: []
.
For example:
- Input: 3, Output:
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
- Input: 1, Output:
1
- Input: 0, Output:
[]
- Input: -1, Output:
[]
All output permutations must be sorted in lexicographic order. Note that a lexicographic order means that the permutation \( A = [a_1, a_2, \dots, a_n] \) comes before \( B = [b_1, b_2, \dots, b_n] \) if there exists an index \( i \) such that \( a_j = b_j \) for every \( j < i \) and \( a_i < b_i \).
inputFormat
The input is provided via standard input (stdin) as a single line containing the value \( n \). This value should be parsed as an integer. If parsing fails or if \( n \) is not greater than 0, the program should output an empty list ([]
).
outputFormat
If \( n \) is a valid positive integer, output each permutation on a separate line to standard output (stdout). The numbers within each permutation must be separated by a single space. If \( n \) is invalid, output []
.
3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
</p>