#K49752. Minimum Swaps to Sort

    ID: 28712 Type: Default 1000ms 256MiB

Minimum Swaps to Sort

Minimum Swaps to Sort

You are given an array of ( n ) integers. Your task is to determine the minimum number of swaps required to sort the array in ascending order. The solution is based on identifying cycles in the permutation of indices. Recall that in a cycle of length ( k ), the number of swaps required is ( k - 1 ).

inputFormat

The input consists of two lines. The first line contains an integer ( n ), representing the number of elements in the array. The second line contains ( n ) space-separated integers representing the array elements.

outputFormat

Output a single integer: the minimum number of swaps required to sort the array in ascending order.## sample

4
4 3 1 2
3

</p>