#C1994. Minimum Swaps to Sort

    ID: 45260 Type: Default 1000ms 256MiB

Minimum Swaps to Sort

Minimum Swaps to Sort

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.

The concept behind the solution is to understand how many cycles the array forms when mapped to its sorted order. If a cycle has k elements, then it requires \(k-1\) swaps to arrange them in order.

For example, consider the array [4, 3, 2, 1, 5]. When sorted, it becomes [1, 2, 3, 4, 5]. With proper cycle analysis, the number of required swaps in this case is 2.

inputFormat

The input starts with an integer T representing the number of test cases. Each test case contains two lines. The first line has an integer N denoting the number of elements in the array. The second line contains N space-separated integers representing the array.

outputFormat

For each test case, print a single integer representing the minimum number of swaps required to sort the given array in ascending order.

## sample
3
5
4 3 2 1 5
4
1 3 5 2
5
1 2 3 4 5
2

2 0

</p>