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.
Hey! Well your solution is a little inefficient even though it passes the test cases. The reason being that in each iteration you are calling numbers.index which is O(n). Instead when you sort the array you can sort the indexes too along with the numbers. Look at what I have done. It makes numbers.index O(n). create an object which pairs up the index and number. then sort based on the number and indexes also get sorted along with it. My solution is O(n) while yours maybe O(n^2).
Minimum Loss
You are viewing a single comment's thread. Return to all comments →
Hey! Well your solution is a little inefficient even though it passes the test cases. The reason being that in each iteration you are calling numbers.index which is O(n). Instead when you sort the array you can sort the indexes too along with the numbers. Look at what I have done. It makes numbers.index O(n). create an object which pairs up the index and number. then sort based on the number and indexes also get sorted along with it. My solution is O(n) while yours maybe O(n^2).