You are viewing a single comment's thread. Return to all comments →
Thanks mate. Nice algo. I didn't understand it at first, but then I solved it using pen and paper using this algo and then got the gist of it.
The idea is to sort the given array and then compare each element of the given array with the sorted array . If the elements in the given array don't match move the offending element to the position of the correct element and bring the correct element in the the position of the offending element. And using a hashmap to store the indexes for O(1) lookup.
Ofcourse this works because there are no duplicate elements in the given array.