#C10655. Minimum Swaps to Sort Array

    ID: 39884 Type: Default 1000ms 256MiB

Minimum Swaps to Sort Array

Minimum Swaps to Sort Array

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.

For a given cycle in the permutation, if the cycle length is (L), then the number of swaps required for that cycle is (L-1). Hence, the total number of swaps is given by (\sum (L_i - 1)), where (L_i) is the length of the (i^{th}) cycle.

This problem will be tested on multiple test cases. Each test case is presented with an integer (n) (the size of the array) followed by (n) space-separated integers.

inputFormat

The first line of the input contains an integer (T) denoting the number of test cases. For each test case, the first line contains a single integer (n), representing the size of the array. The next line contains (n) space-separated integers, representing the elements of the array.

outputFormat

For each test case, output a single line containing one integer — the minimum number of swaps required to sort the array in ascending order.## sample

5
5
4 3 1 2 5
4
1 3 5 2
1
100
6
2 8 5 3 9 4
3
3 1 2
3

2 0 4 2

</p>