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.
If anyone is interested, this is the code I used for python (I was stuck on this for about a week so tried to explain my logic as clear as possible!). Give me some pointers if you have them, relatively new to coding.
# Complete the minimumSwaps function below.defminimumSwaps(arr,n,swaps=0):#iterate over entire arrayforiinrange(0,n):#it's good practice to use a boolean guided function in a long for loop,#while will evaluate and IF the statement in it is true it will continue#I used the consecutive, increasing values to swap by indexwhilearr[i]!=(i+1):#temp is the correct index of arr[i]temp=arr[i]-1#swap this with whatever element is where arr[temp] is, this will#continue to swap until arr[i] == index+1arr[i],arr[temp]=arr[temp],arr[i]#increase swapsswaps+=1returnswaps
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
If anyone is interested, this is the code I used for python (I was stuck on this for about a week so tried to explain my logic as clear as possible!). Give me some pointers if you have them, relatively new to coding.