C++ O(n) Solution | Easy to understand with example | Prefix and Suffix sum
\nvector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        int n=nums.size();\n        vector<int> ans(n,0);\n        vector<int> prefix(n,0);// pref
```\nvector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        int n=nums.size();\n        vector<int> ans(n,0);\n        vector<int> prefix(n,0);// prefix sum\n        vector<int> suffix(n,0);// suffix sum\n        prefix[0]=nums[0];\n        suffix[n-1]=nums[n-1];\n        \n        // prefix sum fill\n        for(int i=1;i<n;i++)\n            prefix[i]=prefix[i-1]+nums[i];\n        \n        // suffix sum fill\n        for(int i=n-2;i>=0;i--)\n            suffix[i]=suffix[i+1]+nums[i];\n        \n        /*\n         Eg: nums = [1,4,6,8,10]\n         \n         for i=2 i.e element = 6\n         ans[i] will be as follows:-\n=>  6-1 + 6-4 + 6-6                         +   8-6 + 10-6\n=>  6*3 - (1+4+6)                           +   (8+10)-6*2\n=>  nums[i]*(index+1) - prefixsum upto i    +   suffix sum of elements after ith index - nums[i]*number of elements after ith index          \n          \n        */\n        for(int i=0;i<n;i++)\n        {\n            ans[i]=nums[i]*(i+1)-prefix[i];\n            if(i!=n-1)// since last element does not have any suffix sum\n                ans[i]+=suffix[i+1]-nums[i]*(n-1-i);\n        }\n        return ans;       \n    }\n```
['C', 'Prefix Sum']