#C3522. Minimum Number of Swaps to Sort

    ID: 46959 Type: Default 1000ms 256MiB

Minimum Number of Swaps to Sort

Minimum Number of Swaps to Sort

Given an array of distinct integers, determine the minimum number of swaps required to sort the array in ascending order. The approach is based on cycle detection: for each cycle of misplaced elements, if the cycle size is \( k \), then exactly \( k - 1 \) swaps are needed to put them in the correct positions.

You are given an integer \( n \) representing the number of elements, followed by \( n \) space-separated integers. Compute and output the minimum number of swaps required to sort the array.

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 representing the minimum number of swaps required to sort the array in ascending order.

## sample
4
4 3 2 1
2