Transform a permutation so the first positions become the lexicographically smallest possible sequence after at most one swap per position.
You are given a permutation of integers from $1n$. You may perform a sequence of swaps, but each position can be involved in at most one swap.
Your task is to make the permutation as small as possible in lexicographic order under this constraint.
A lexicographically smaller permutation is one that differs at the first position where the two permutations are different, and has the smaller value there.
Process the permutation from left to right and use swaps only when they improve the prefix as much as possible, while respecting the limit that every index can be used at most once.
This is a classic greedy permutation-rearrangement problem.
Example 1
Input
5 5 4 3 2 1
Output
1 2 3 4 5
Explanation
Swapping elements greedily from the left lets the smallest values move to the front while using each position at most once.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.