Prefix and suffix sum cpp
Code\n\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        // int sum=accumulate(nums.begin(),nums.end(),0);\n  
# Code\n```\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        // int sum=accumulate(nums.begin(),nums.end(),0);\n        vector<int>ans;\n        int n=nums.size();\n\n        vector<int>pre(n,0);\n        vector<int>suf(n,0);\n        pre[0]=nums[0];\n        suf[n-1]=nums[n-1];\n        for(int i=1;i<n;i++)\n        {\n            pre[i]=pre[i-1]+nums[i];\n        }\n\n        for(int i=n-2;i>=0;i--)\n        {\n            suf[i]=suf[i+1]+nums[i];\n        }\n\n        for(int i=0;i<n;i++)\n        {\n            int temp=(i+1)*nums[i]-pre[i];\n            temp+=(suf[i]-((n-i)*nums[i]));\n            ans.push_back(temp);\n        }\n        return ans;\n    }\n};\n```
['C++']