1685. Sum of Absolute Differences in a Sorted Array

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1685

class Solution:
    def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:
        res = []
        s, t = sum(nums), 0
        for i, x in enumerate(nums):
            v = x * i - t + s - t - x * (len(nums) - i)
            res.append(v)
            t += x
        return res