#K92202. Minimum Swaps to Sort
Minimum Swaps to Sort
Minimum Swaps to Sort
You are given a string that represents a list of comma-separated integers. Your task is to determine the minimum number of swaps required to sort the array in ascending order.
The key observation is that the array can be divided into cycles. For each cycle with k elements, the minimum number of swaps required is \(k-1\). The overall answer is the sum of the swaps required for each cycle.
For example, given the input "4,3,2,1", the minimum number of swaps is 2.
inputFormat
The input is provided on standard input (stdin) as a single line. The line contains a string of comma-separated integers. It may be empty, in which case the answer is 0.
Example: 4,3,2,1
outputFormat
The output should be a single integer printed to the standard output (stdout) representing the minimum number of swaps required to sort the numbers in ascending order.
Example: 2
1,2,3,4
0