You are viewing a single comment's thread. Return to all comments →
Don think we need two arrays..Here is a solution static int minimumSwaps(int[] arr) {
boolean swapsNeeded=true; int swapLocation,temp,nSwaps = 0; while(swapsNeeded) { swapsNeeded = false; for(int i=0; i<arr.length;i++) { if(arr[i]!=(i+1)) { swapLocation = arr[i]-1; swapsNeeded=true; temp = arr[i]; arr[i] = arr[swapLocation]; arr[swapLocation] =temp; nSwaps++; } } } return nSwaps; }
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 →
Don think we need two arrays..Here is a solution static int minimumSwaps(int[] arr) {