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.
Yes, but searching takes more time, as you said, up to n^2.
That's why I chose to have one more array wich would hold the indeces of arr:
arr2[arr[i]-1]=i;
which is similar to a map.
In this case the time is 2n.
Most of the hackerrank tests have a time-out error, if the algorithm is not the most efficient, that's why I choose efficiency as priority.
Cookie support is required to access HackerRank
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 →
Yes, but searching takes more time, as you said, up to n^2. That's why I chose to have one more array wich would hold the indeces of arr: arr2[arr[i]-1]=i;
which is similar to a map.
In this case the time is 2n.
Most of the hackerrank tests have a time-out error, if the algorithm is not the most efficient, that's why I choose efficiency as priority.