Leaderboard
Showing:
Handle Score
@Ibrahim Nash 5725
@blackshadows 5685
@akhayrutdinov 5111
@mb1973 4989
@Quandray 4908
@saiujwal13083 4156
@sanjay05 3762
@marius_valentin_dragoi 3511
@sushant_a 3453
@KshamaGupta 3318
@the_coder95 3302
Complete Leaderboard
Minimum Swaps to Sort
Medium Accuracy: 50.0% Submissions: 32705 Points: 4

Given an array of size N. Find the minimum number of swaps required to sort the array in non-decreasing order.


Example 1:

Input: 
N = 5
arr = {1 5 4 3 2}
Output: 2
Explaination: swap 2 with 5 and 3 with 4.

 

Example 2:

Input: 
N = 4
arr[] = {8 9 16 15}
Output: 1
Explaination: swap 16 and 15.


Your Task:
You do not need to read input or print anything. Your task is to complete the function minSwaps() which takes the array arr[] and its size N as input parameters and returns an integer denoting the minimum number of swaps required to sort the array. If the array is already sorted, return 0. 


Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N)

 

Constraints:
1 <= N <= 105
1 <= A[] <= 106

 

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Minimum Swaps to Sort

Output Window