The players are currently in a random, disorganized line, and Imran wants the MINIMUM number of swaps needed to sort them by height with the least reshuffling possible. Read N distinct integers (a permutation of 1..N is NOT guaranteed, just distinct values). Find the minimum number of swaps needed to sort the array ascending.
Input
First line N, second line N distinct integers.
Output
Minimum number of swaps.
Constraints
1 ≤ N ≤ 10^5
Sample Input
4 4 3 2 1
Sample Output
2
main.c
Loading editor…
Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.