You are given an integer array nums sorted in non-decreasing order.

Build and return an integer array result with the same length as nums such that result[i] is equal to the summation of absolute differences between nums[i] and all the other elements in the array.

In other words, result[i] is equal to sum(|nums[i]-nums[j]|) where 0 &lt;= j &lt; nums.length and j != i (0-indexed).

&nbsp;
Example 1:

Input: nums = [2,3,5]
Output: [4,3,5]
Explanation: Assuming the arrays are 0-indexed, then
result[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,
result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3,
result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.


Example 2:

Input: nums = [1,4,6,8,10]
Output: [24,15,13,15,21]


&nbsp;
Constraints:


	2 &lt;= nums.length &lt;= 105
	1 &lt;= nums[i] &lt;= nums[i + 1] &lt;= 104


from itertools import accumulate 

class Solution(object):
    def getSumAbsoluteDifferences(self, nums):
        total, n = sum(nums), len(nums) #for i, ri in zip(nums, reversed(nums)): pref.append(pref[-1] + i)
        return [(((i+1) * num) - pref) + ((total-pref) - ((n-i-1) * num)) for (i, num), pref in zip(enumerate(nums), list(accumulate(nums)))]
        
class Solution {
    public int[] getSumAbsoluteDifferences(int[] nums) {
        int n = nums.length;
        int[] res = new int[n];
        int sumBelow = 0;
        int sumTotal = Arrays.stream(nums).sum();

        for (int i = 0; i < n; i++) {
            int num = nums[i];
            sumTotal -= num;
            res[i] = sumTotal - (n - i - 1) * num + i * num - sumBelow;
            sumBelow += num;
        }
        return res;
    }
}
class Solution {
public:
    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {
        vector<int>ans(nums.size(),0);
        for(int i = 1;i<nums.size();i++)
            ans[0]+=(nums[i]-nums[0]);

        for(int j = 1;j<nums.size();j++)
            ans[j] = ans[j-1]+(nums[j]-nums[j-1])*j-(nums[j]-nums[j-1])*(nums.size()-j);

        return ans;
    }
};
var getSumAbsoluteDifferences = function(nums) {
    const N = nums.length;
    const ans = new Array(N);
    ans[0] = nums.reduce((a, b) => a + b, 0) - (N * nums[0]);
    for (let i = 1; i < N; i++)
        ans[i] = ans[i - 1] + (nums[i] - nums[i - 1]) * i - (nums[i] - nums[i - 1]) * (N - i); 
    return ans;
};
Sum of Absolute Differences in a Sorted Array