Python Solution O(n) || Using Prefix and Suffix Sum || Easy to understand
\nclass Solution:\n    def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:\n        # initialize two variables for keep updating prefix (s) and s
```\nclass Solution:\n    def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:\n        # initialize two variables for keep updating prefix (s) and suffix sums(s1).\n\t\ts,s1 = 0,sum(nums)\n\t\t\n\t\t# lists to store prefix and suffix sums at each indexes\n        prefix,suffix = [],[]\n\t\t\n\t\t# calculating prefix and suffix sums of array\n        for num in nums:\n            s += num\n            prefix.append(s)\n            suffix.append(s1)\n            s1 -= num\n\t\t\t\n        ans = []\n        for i in range(len(nums)):\n\t\t\t# from index i all elements till n have higher or equal value since it\'s sorted\n\t\t\t# so |nums[i]-nums[j]| for j>i will equalent to (nums[i]-nums[j])\n\t\t\t# for numbers j<i it will be equalent to (nums[j]-nums[i])\n\t\t\t\n\t\t\t# find the right side sum and difference\n            sufval = suffix[i]-nums[i]*(len(nums)-i)\n\t\t\t\n\t\t\t# find the left side sum and difference\n            preval = nums[i]*(i+1) - prefix[i]\n\t\t\t\n\t\t\t# append it to the ans\n            ans.append(sufval+preval)\n        return ans\n```
['Array', 'Math', 'Prefix Sum', 'Python', 'Python3']