Given an array of values, I want to find a general “score”, where the rating of each element is the number of elements with a lower value that appears before it in the array.
eg.
values: 4 1 3 2 5
scores: 0 0 1 1 4
total score: 6
The O (n ^ 2) algorithm is trivial, but I suspect that it is possible to do this in O (nlgn), by sorting the array. Does anyone have any ideas how to do this, or if this is not possible?
source
share