I have a sorted array of distinct positive integers. For each element in the array, I would like to know the total distance between that element and every other element in the array. I will return those distances in its own array.
For example, let's say the array is [2, 3, 5]
dist(2) = |2 - 3| + |2 - 5| = 4
dist(3) = |3 - 2| + |3 - 5| = 3
dist(5) = |5 - 2| + |5 - 3| = 5
Therefore, I would return [4, 3, 5]
It's pretty trivial to compute that array in quadratic time. Is it possible to do it in linear time?