Problem:
https://www.hackerrank.com/challenges/minimum-swaps-2/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays

Solution:

def minimumSwaps(arr):
    count=0
    i=0
    while i< len(arr):
        index=arr[i]-1   # n= arr[i]       index=n-1=arr[i]-1 
       # print('bef=',arr,i)
        if arr[i]!=arr[index]: #n!=[n-1]
            arr[i],arr[index]=arr[index],arr[i]
            count+=1
        else:
            i+=1
       # print('aft=',arr)    
    return count  

Explanation:

NOTE:
You can also join our membership for clearning your doubts or for any personal learning for Python , C , Data Structure using C or Python. Link is below:

https://www.youtube.com/channel/UCj98X_6ZxT9Xcx7NO0ECXZw/join

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top