Simple && Fast 🚀 => Beats more than 99%
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nHi Coders!\nFirst, we d
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHi Coders!\nFirst, we did a Prefix Sum on another array that has a Big initial value.\nfor Each element of the array, the sum will be the sum of 2 sides (right & left)\nyou make a number of operations, every operation you subtract an element from $$nums_i$$\n1- the sum of the previous will be `(curr * count of previous numbers)  - sum of previous numbers`.\n1- the sum of the next will be `(curr * count of next numbers)  - sum of next numbers`.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n# Code\n```\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        ios_base::sync_with_stdio(false), cout.tie(NULL), cin.tie(NULL);\n        static const int N=1e5+5;\n        vector<int>res;\n        int ans,v;\n        int a[N]={};\n        for(int i=1;i<=nums.size();i++)\n        {\n            a[i]=nums[i-1];\n            a[i]+=a[i-1];\n        }\n        for(int i=1;i<=nums.size();i++)\n        {\n            \n            int cur=a[i]-a[i-1]; // 2 3 5\n            ans=abs(cur*(i-1)-a[i-1]); // 0 1 5\n            v=(a[nums.size()]-a[i])-(cur*(nums.size()-i)); // 4 2 0\n            ans+=abs(v);\n            res.push_back(ans);\n        }return res;\n    }\n};\n```
['Math', 'Prefix Sum', 'C++']