Permutations

Practice this coding question on Coding Gurukul.

Problem statement

A robotics lab must try every visit order for a small set of stations. Each station has a distinct integer label. A trial visits every station exactly once, so two trials are different whenever their ordered sequences differ at any position. Your task Generate all permutations of the given distinct integers. Print their count and then print each permutation on a separate line, in numeric lexicographic order: at the first differing position, the sequence with the smaller integer comes first. Use every input value exactly once in each row. Do not sort only the printed text, because negative and multi-digit numbers must be compared as integers. The input may arrive in any order; it must not change the ordered output. A single station produces exactly one trial. Worked example For [4, 2], print count 2 and the rows 2 4 and 4 2. Both visit the same two stations, but in different orders. For a single label -3, the count is 1 and the only row is -3.

Input format

First line: n. Second line: n distinct integers.

Output format

Print the permutation count, followed by all permutations in numeric lexicographic order.

Constraints

1 <= n <= 6; -10 <= each value <= 10; values are distinct.

Sample input

2
4 2

Sample output

2
2 4
4 2

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…