Python 3 | O(N), Prefix Sum, Clean | Explanation
Explanation\n- By definition, difference between a and b = abs(a - b) = a - b if a > b else b - a\n- What we will do is to take \n\t- num * (i+1) - prefix_sum b
### Explanation\n- By definition, `difference between a and b = abs(a - b) = a - b if a > b else b - a`\n- What we will do is to take \n\t- `num * (i+1) - prefix_sum before i`\n\t- `prefix_sum_after_i - num * (n - i)`\n\t- For example, `nums = [2,4,5,7,8]`, given `i = 2`\n\t\t- We can calculate the prefix sum as `[2,6,11,18,26]`, `n = len(nums) = 5`\n\t\t- `ans[2] = (nums[2] * 3 - 11) + (26-11 - nums[2] * (5-2-1)) = 5 * 3 - 11 + 15 - 5 * 2 = 4 + 5 = 9`\n### Implementation\n```\nclass Solution:\n    def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:\n        pre_sum = [0]\n        for num in nums:                                     # calculate prefix sum\n            pre_sum.append(pre_sum[-1] + num)\n        n = len(nums)                                        # render the output\n        return [(num*(i+1) - pre_sum[i+1]) + (pre_sum[-1]-pre_sum[i] - (n-i)*num) for i, num in enumerate(nums)]\n```
['Array', 'Prefix Sum', 'Python', 'Python3']