Update appNew update is available. Click here to update.

Minimum swaps to sort Array

Last Updated: 3 Jul, 2021
Difficulty: Easy

PROBLEM STATEMENT

Try Problem

Given an array 'arr' of size 'N', find the minimum number of swaps required to be made between any two elements to sort the array in ascending order. The array does not contain duplicates i.e all the elements of the array are distinct.

Example :

Let the given array be [1,5,4,3,2]  we swap index 1 with 4 i.e. 5 -> 2 and 2 with 3 i.e. 4 -> 3 to form the sorted array {1, 2, 3, 4, 5}.
Input Format:
The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow.

The first line of each test case contains an integer ‘N’ representing the size of the input array.

The second line of each test case contains the 'N' elements of the array separated by a single space.
Output Format:
For each test case, print a single line containing a single integer which represents the minimum no. of swaps required to sort the array.

The output for each test case is in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= N <= 1000
0 <= ARR[i] <= 10 ^ 9

Where 'ARR[i]' is the value of the input array elements.

Time Limit: 1 sec