Amazon | Onsite | SDE I

Anonymous User
770
Sep 25, 2024
Sep 26, 2024

I have an array of numbers (not necessarily sorted). How do I return an array of the first index of each number if it was a sorted array?

nums = [8,5,4,3,1]

answer = [4,3,2,1,0]

For each number, you replace it with the first index it would have in a sorted array.

nums = [9, 9, 5, 4, 2, 1]

answer = [4, 4, 3, 2, 1, 0]

Is it possible to do this faster than O(nlogn)? Space does not matter.

Comments (7)