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.
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.
Cheers!
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lily's Homework
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.
Cheers!