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.
// Complete the minimumSwaps function below.staticintminimumSwaps(int[]arr){intn=arr.length;ints=0;Map<Integer,Integer>m=newHashMap<>();for(inti=0;i<n;i++){m.put(arr[i],i);}for(inti=0;i<n;i++){//loop//look for minintmin=i+1;intminIndex=m.get(min);if(min!=arr[i]){//update mapm.put(arr[i],minIndex);//swapinttemp=arr[i];arr[i]=arr[minIndex];arr[minIndex]=temp;s++;}}returns;}
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 →