Easy C++ solution using prefix and suffix sum array
\nans = (nums[i] * i - (nums[0] + nums[1] + ... + nums[i-1])) + ((nums[i+1] + nums[i+2] + ... + nums[n-1]) - nums[i] * (n-i-1))\n\n (nums[0] + nums[1] + ... + n
\nans = (nums[i] * i - (nums[0] + nums[1] + ... + nums[i-1])) + ((nums[i+1] + nums[i+2] + ... + nums[n-1]) - nums[i] * (n-i-1))\n\n **(nums[0] + nums[1] + ... + nums[i-1]) -> prefixSum[i-1];\n (nums[i+1] + nums[i+2] + ... + nums[n-1]) -> suffixSum[i+1];**\n Refer my code below:\n \n```\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        vector<int>ans;\n        int n=nums.size();\n        \n        vector<int>prefixSum(n,0);\n        prefixSum[0]=nums[0];\n        \n        vector<int>suffixSum(n,0);\n        suffixSum[n-1]=nums[n-1];\n        \n        for(int i=1; i<n; i++){\n            prefixSum[i]=prefixSum[i-1]+nums[i];\n        }\n        \n        for(int i=n-2; i>=0; i--){\n            suffixSum[i]=suffixSum[i+1]+nums[i];\n        }\n        \n        int sum=(suffixSum[0]-nums[0]*n);\n        ans.push_back(sum);\n        \n        for(int i=1; i<n-1; i++){\n            sum=((nums[i]*i)-prefixSum[i-1])+(suffixSum[i+1]-(nums[i]*(n-i-1)));\n            ans.push_back(sum);\n        }\n        \n        sum=(nums[n-1]*(n)-prefixSum[n-1]);\n        ans.push_back(sum);\n        return ans;\n    }\n};\n```
[]