easy +8 pts

Argsort Indices

Return the indices that would sort a list of integers, with ties broken by original order.

Write a function `argsort_indices(arr)` that takes a list of integers `arr` and returns a list of indices that would sort the list in ascending order. When two elements are equal, their indices should appear in the original order (stable sort). The returned list contains the indices `0` through `len(arr)-1` rearranged so that `arr[result[i]]` is the i-th smallest element. For example, for `arr = [3, 1, 2]`, the indices are `[1, 2, 0]` because `arr[1]=1`, `arr[2]=2`, `arr[0]=3`. The input list must not be modified.

Constraints

The input list length is between 0 and 1000. Elements are integers within Python's standard range. The solution must run in O(n log n) time or better and use O(n) extra space.

Example

>>> argsort_indices([3, 1, 2])
[1, 2, 0]
>>> argsort_indices([5, 5, 5])
[0, 1, 2]
>>> argsort_indices([])
[]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `sorted` with a key that returns the element and the index as a tuple to get a stable sort by value.
After sorting pairs of (value, index), extract the index from each pair.
For an empty list, return an empty list.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.