#C5338. Minimum Swaps to Sort Potions
Minimum Swaps to Sort Potions
Minimum Swaps to Sort Potions
In Magic Land, potions are stored in bottles with varying volumes. The wizards of the land need your help to organize these potions in non-decreasing order using a series of swaps. In one swap, you can exchange the contents of any two bottles.
Your task is to determine the minimum number of swaps required to sort an array of potion volumes in non-decreasing order.
Formally, given an array \(A = [a_1, a_2, \dots, a_n]\), find the minimum number of swaps needed to sort the array in ascending order.
inputFormat
The input begins with a single integer \(T\) representing the number of test cases. Each test case consists of the following two lines:
- The first line contains an integer \(N\), the number of bottles.
- The second line contains \(N\) space-separated integers representing the volumes of the potions.
outputFormat
For each test case, output a single integer on a new line — the minimum number of swaps required to sort the potion volumes in non-decreasing order.
## sample2
5
4 3 1 2 5
3
3 1 2
3
2
</p>