#C2036. Minimum Swaps to Sort

    ID: 45308 Type: Default 1000ms 256MiB

Minimum Swaps to Sort

Minimum Swaps to Sort

You are given an array of distinct integers. Your task is to determine the minimum number of swaps required to sort the array in ascending order.

The solution involves finding cycles in the permutation of the array. For every cycle of length l, the number of swaps needed is l - 1.

Hint: Consider representing the array elements with their positions and sort these pairs to track the cycles.

inputFormat

The first line contains an integer N representing the number of elements in the array. The second line contains N space-separated integers.

outputFormat

Output a single integer which is the minimum number of swaps required to sort the array in ascending order.

## sample
4
4 3 1 2
3