dankealot's solution: C++ Prefix Sum & Suffix Sum
Question: We have a vector and from each element we want to subtract all other\'s element including the element itself. In other words, (since the elements are 
**Question**: We have a vector and from each element we want to subtract all other\'s element including the element itself. In other words, (since the elements are sorted) we want to find difference of sum of elements after an element and sum of elements before that element. ( As subtracting number from itself remains 0 so no need to add it)\n\n<hr>\n\n**Approach**: Create a prefix sum and suffix sum. Now create an answer vector and insert the value of sum into it.\n\nNow how to find the sum?\n\nEx: a = [1,4,6,8,10]\nFor i=0, sum =  abs(1-1) + abs(1-4) + abs(1-6) + abs(1-8) + abs(1-10)\n\nNow we know that array is sorted so , elements after a particular element will be greater than current element.\nInstead of writing 1-4 for index 1,  we can write 4-1 and for elements above.\nSo, we can say:\nsum = a[0]-a[0] + a[1]-a[0] + a[2]-a[0] + a[3]-a[0] + a[4]-a[0]\nObserving this equation, we can see that\nfor i =0 , sum = (a[1] + a[2] + a[3] + a[4]) - (a[0] + a[0] + a[0] + a[0])  i.e. sum of elements on right side of index 0 and occurence of a[0] for which we have to subtract.\n\nSimilarly for i=1, we will first write value at index 1 then subtract elements for index less than 1\nsum = a[1]-a[0] + a[1]-a[1] + a[2]-a[1] + a[3]-a[1] + a[4]-a[1]\nAgain, observing this equation, we can see that\nfor i =1 , sum = ( a[2] + a[3] + a[4]) - (a[1] + a[1] + a[0])  i.e. sum of elements on right side of index 1 and occurence of a[1] for which we have to subtract.\n\nIn mathematical form, it can be written as:\n**sum = right - left - element * (abs( i - (n-i-1) ))**\nwhere,\n**right\nleft\nterm** = sum of elements on the right side of a particular index\n**right\nleft\nterm** = sum of elements on the left side of a particular index\nand the **right\nleft\nterm**:  element * ( i - (n-i-1) ) tells us the occurences of current element\n\ni.e. ex: for i =1  sum = (a[2] + a[3] + a[4]) - (a[0]) - (a[1] + a[1])\nHere number of a[1] that is current element occurs 3 times, which can be easily proved as in:\nsum = a[1]-a[0] + a[2]-a[1] + a[3]-a[1] + a[4]-a[1]   :   **we have count of positive a[1] as 1 and -a[1] as 3\nso we are left with 2 count of a[1]**\n\nSo, here (a[2] + a[3] + a[4]) is the sum of right elements of index 1,\na[0] is the sum of elements of index1 and -(a[1]*2) tells about the total occurences of a[1] i.e. **current element while calculating the difference from all the elements.**\n\n**However**, while traversing from left to right, after traversing half array , the value of right-left is less than the third term in the equation.\n\nSo, final solution is:\nfor i <n/2 ,  sum = (right - left) - element * (abs( i - (n-i-1) ))\nfor i>=n/2,  sum = (right - left) + element * (abs( i - (n-i-1) ))\n\nAlso, beware of **edge cases**, if i = 0 , then left sum is to taken as 0 and if i==n-1, then right sum is to be taken as 0.\n\n\n**Do upvote, If u understood the logic \uD83D\uDE00\nDo comment if not...**\n\n```\n#define ifr(i,a,b) for(int i=a;i<b;i++)\n#define pb push_back\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        int n = nums.size();\n        int sum = accumulate(nums.begin(),nums.end(),0);\n        int pre[n];\n        int suf[n];\n        ifr(i,0,n){\n            if(i==0){\n                pre[i] = nums[i];\n                suf[i] = sum;\n            }\n            else{\n                pre[i] = pre[i-1] + nums[i];\n                suf[i] = suf[i-1] - nums[i-1];\n            }\n        }\n\n        int r=0,l=0;\n        vector<int> ans;\n        ifr(i,0,n){\n            int sum = 0;\n            if(i+1<n){\n                r = suf[i+1];\n            }\n            if(i-1>=0){\n                l = pre[i-1];\n            }\n            if(i<n/2){\n                sum = r - l - nums[i]*(abs(i-(n-i-1)));\n            }\n            else{\n                sum = r - l + nums[i]*(abs(i-(n-i-1)));\n            }\n            ans.pb(sum);\n            r = 0;\n            l = 0;\n        }\n        return ans;\n    }\n};\n```
['C', 'Prefix Sum']