We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Just sort and count number of swaps required to get the sorted array
For some test cases reverse of sorted array gives minimum swaps.
So I have calculated swaps for both sorted ones and its reverse
deflilysHomework(arr):# Write your code herearrs=sorted(arr)d1={}d2={}foriinrange(len(arr)):d1[arr[i]]=arrs[i]d2[arr[i]]=arrs[len(arr)-1-i]returnmin(getswaps(d1),getswaps(d2))defgetswaps(d):swap=0forxind.keys():y=d[x]while(x!=y):swap+=1d[x]=d[y]d[y]=yy=d[x]returnswap
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lily's Homework
You are viewing a single comment's thread. Return to all comments →
Just sort and count number of swaps required to get the sorted array For some test cases reverse of sorted array gives minimum swaps. So I have calculated swaps for both sorted ones and its reverse