You are viewing a single comment's thread. Return to all comments →
//to handle non consecutive numbers
map<int, int> M; for(int i = 0; i < arr.size(); i++) M[arr[i]] = i; map<int, int>::iterator it; int j = 0, swipe = 0; for(it = M.begin(); it != M.end(); ++it){ if(arr[j] != (*it).first){ int k = (*it).second; swap(M[arr[j]], M[(*it).first]); swap(arr[j], arr[k]); swipe++; } j++; } return swipe;
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 →
//to handle non consecutive numbers