Loading...
Loading...
Loading...
A shelf holds boxes with distinct weights. In one move you may swap any two boxes, not only neighbours. Print the smallest number of swaps that puts the boxes in increasing order of weight.
Example 1
Input
4 3 2 1
Output
2
Example 2
Input
1 5 4 3 2
Output
2
Try the problem first. Open one hint at a time.
Find where each value belongs after sorting. The positions form cycles.
A cycle of length L needs L - 1 swaps.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor