O(n) || 2 pass solution || Short Code || Explained
Upvote if this post is useful\n\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        int tsum= 0; \n        for(a
**Upvote** if this post is useful\n```\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        int tsum= 0; \n        for(auto it : nums) tsum+=it; //calculating total sum\n        int n = nums.size();\n        int csum = 0; //this will strore cumulative sum to current index\n        vector<int> ans(n);\n        for(int i = 0;i<n;i++){\n            csum += nums[i];\n\t\t\t//there will be two components of ans[i]\n\t\t\t//we know for j<i , nums[j]<nums[i] ,therefore left sum of absolute difference is calculated like (nums[i]*(i+1)-csum)\n\t\t\t//whereas for j>i,nums[j]>nums[i],therefore right sum of absolute difference is calculated like rsum-(nums[i]*(n-1-i))\n\t\t\tint rsum = tsum-csum; //remaining sum\n            ans[i] = (nums[i]*(i+1)-csum) + rsum-(nums[i]*(n-1-i));\n        }\n        return ans;\n    }\n};\n```
['C', 'Prefix Sum']