26
2
Summary
Given a list of integers, return the index each integer would end up at when sorted.
For example, if the list was [0,8,-1,5,8]
, you should return [1,3,0,2,4]
. Note that the two 8
s maintain their order relative to each other (the sort is stable).
Put another way: For each element in the list, return the number of elements in the list that are: Smaller than the chosen element OR (equal to the element AND appears before the chosen element)
Indexes must start with 0 (not 1)EDIT: given the large pushback, I'll allow 1-based indicies.
Test cases:
0 -> 0
23 -> 0
2,3 -> 0,1
3,2 -> 1,0
2,2 -> 0,1
8,10,4,-1,-1,8 -> 3,5,2,0,1,4
0,1,2,3,4,5,6,7 -> 0,1,2,3,4,5,6,7
7,6,5,4,3,2,1,0 -> 7,6,5,4,3,2,1,0
4,4,0,1,1,2,0,1 -> 6,7,0,2,3,5,1,4
1,1,1,1,1,1,1,1 -> 0,1,2,3,4,5,6,7
1,1,1,1,1,1,1,0 -> 1,2,3,4,5,6,7,0
Despite the simpleness of this challenge, I couldn't find a duplicate of this challenge. – Nathan Merrill – 2016-07-19T18:05:06.320
1
This is a specialisation of this question where instead of taking two arrays it takes one array and the second one is
– Peter Taylor – 2016-07-19T18:28:48.207[0 1 ... n-1]
.@PeterTaylor: In that challenge, the array has no repeats. – Lynn – 2016-07-19T18:32:26.540
2Note to solvers: that
8,10,4,-1,-1
test case is very deceptive. Try the4,4,0,1,1,2,0,1
one first. – Lynn – 2016-07-19T18:59:24.660@Lynn I looked up what "grade up" does, and I figured out why that test case is so deceptive. Fixed. – Nathan Merrill – 2016-07-19T19:46:38.963
Related: Get the indices of an array after sorting
– sergiol – 2018-03-26T09:48:10.367