You are viewing a single comment's thread. Return to all comments →
you can just replace the (i+1) in if condition with sorted_arr[i].
vector sorted_arr(arr.begin(),arr.end()); sort(sorted_arr.begin(),sorted_arr.end());
map the index of all elements
mapping[ sorted_arr[i] ] = i;
and swap part will be replaced by
swap(arr[i], arr[ mapping[ arr[i] ] ]);
i think it should work for general elements in array
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 →
you can just replace the (i+1) in if condition with sorted_arr[i].
map the index of all elements
and swap part will be replaced by
i think it should work for general elements in array