#K9606. Minimum Swaps to Sort an Array
Minimum Swaps to Sort an Array
Minimum Swaps to Sort an Array
You are given an array of n distinct integers. Your task is to determine the minimum number of swaps required to sort the array in ascending order.
A swap is defined as exchanging the positions of any two elements. The optimal solution uses cycle detection in the permutation representation of the array to count the necessary swaps.
If the array is already sorted, the result should be 0.
This problem tests your understanding of greedy algorithms and cycle detection.
inputFormat
The first line of input contains an integer n indicating the number of elements in the array.
The second line contains n space-separated integers representing the elements of the array.
outputFormat
Output a single integer representing the minimum number of swaps needed to sort the array in ascending order.
## sample4
4 3 1 2
3
</p>