sum of absolute differences of a number in an array

前端 未结 2 1999
野的像风
野的像风 2021-02-04 05:56

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^

2条回答
  •  逝去的感伤
    2021-02-04 06:18

    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.

提交回复
热议问题