Two Pointers Solution | Beats 100% of C++ users.
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n This problem depends mainly on how to be able to form a mathmatical equation to 
![image.png](https://assets.leetcode.com/users/images/e693cf5a-4483-4a72-bdef-4d814940d5c2_1700900577.7908828.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n* This problem depends mainly on how to be able to form a mathmatical equation to be able to get the sum using the previously evaluated, and the remaining numbers to be evaluated.\n* So we will need two pointers, one to carry the prefix, and the othre to carry the suffex. \n* And we have a note, that the given array is sorted, so this will help us to be able to evaluate the result correctly, and if it was not sorted, then we will need to sort it, and this will fit in our time, since the given constrains allows us to work in O(nlgn). \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Firstly, we need to evaluate the total sum of the whole array, by iterating over all elements in the given list, and count it with us. \n2. then, we need to define **left pointer** start counting from 0.\n3. and another **right pointer** start counting from the total sum.\n4. then we need to declare a result vectors, in which we need to store our results. \n5. then we need to  iterate over the size of the given list.\n    1. The equation we need to solve this problem is \n        ```\n        res[i] = num[i]*i - left + right - (num[i] * (size - i - 1))\n        ```  \n    2. but we need to take care here to not to subtract or add the current element twice, so we have two options\n        1. subtract the current element from right, then apply the equation, then add the current element to the left.\n        2.  add the current element to left, then apply the equation, then subtract the current element from the right.\n        3. but in anyways, we can not do the same operations neither before applying the equaiton, nor after it, to avoid miss calculations.\n    3. then we should update the other pointer. \n6. after we finish doing this, we just need to return the result vector.   \n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n1. O(n) to evaluate the sum.\n2. O(n) to evaluate the result.\n3. overall O(n). \n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n1. O(n) for the result vector.\n# Code\n```\nclass Solution {\npublic:\n\n#define DPSolver                      \\\n    ios_base::sync_with_stdio(false); \\\n    cin.tie(NULL);                    \\\n    cout.tie(NULL)\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        DPSolver;\n        int sum = 0;  \n        for (int num : nums)\n            sum += num; \n        int left = 0; \n        int right = sum; \n        int sz = nums.size(); \n        vector<int> res (sz); \n        for (int i = 0 ; i < sz; i++) {\n            int n = nums[i]; \n            left +=n; \n            res[i] = (n*i) - left + right - (n * (sz - i-1));  \n            right -=n; \n        }\n        return res; \n    }\n};\n```
['Array', 'Math', 'Prefix Sum', 'C++']