I want to calculate the sum of absolute differences of a number at index i with all integers up to index i-1 in o(n). But i am not able to think of any approach better than o(n^
Here's an Omega(n log n)
-comparison lower bound in the linear decision tree model. This rules out the possibility of a "nice" o(n log n)
-time algorithm (two now-deleted answers both were in this class).
There is a trivial reduction to this problem from the problem of computing
f(x1, ..., xn) = sum_i sum_j |xi - xj|.
The function f
is totally differentiable at x1, ..., xn
if and only if x1, ..., xn
are pairwise distinct. The set where f
is totally differentiable thus has n!
connected components, of which each leaf of the decision tree can handle at most one.