[C++] Linear Solution Explained, ~20% Time, ~20% Space
This was probably a bit harder than it was ranked for, but even more so it was a pleasure to solve it :)\n\nIn order to proceed, I created 4 support variables:\
This was probably a bit harder than it was ranked for, but even more so it was a pleasure to solve it :)\n\nIn order to proceed, I created 4 support variables:\n* `len` will store the length of our input;\n* `left` and `right` will store the partial cumulative sum of all the elements on either side of the currently considered node - assuming we start from an ideal node before the the actual first one, we will initialise them to be `0` and the sum of all the elements in `nums`, respectively;\n* `res` is our accumulator variable that we will populate with the partial sums, step by step - giving it an initial size `== len`, to avoid unnecessary and expensive reallocations later.\n\nThen we go looping through all the elements in `nums` by the index `i` doing:\n* an update of `right`, removing `nums[i]` from it;\n* an update of `res`, with the value of `right - left - (len - (i << 1) - 1) * nums[i]` - see more below;\n* an update of `left`, adding `nums[i]` to it.\n\nNow, about updating `res`, we will have that at each given time, for an element in the `i`th position its value is equal to the `i ` times `nums[i]`, minus the partial sum of all the values to its left (`left`), plus the partial sum of all the values to its `right` minus all the remaining items (`len - i - 1`) times `nums[i]`.\n\nLet\'s check with an example, like the provided input of `{1,4,6,8,10}`; for the first element, position `0`, we will have:\n\n```cpp\n// now: left = 0, right = 28\n0 // on its left\n|1 - 4| + |1 - 6| + |1 - 8| + |1 - 10| // on the right\n```\n\nThe part on the right can be further grouped as the sum of all the elements on its `right` (which in our code will be equal to the sum of all the elements, minus the freshly removed `nums[0]`, that is to say `4 + 6 + 8 + 10`) minus `nums[i]` (`1`) multiplied by the number of all elements on its right `len - i - 1`, which is `4` in our case, so, to recap: `4 + 6 + 8 + 10 - 4 * 1` - which is precisely what we want to write in `res[0]`!\n\nWe update `left` adding `nums[0]`, start another loop having `right -= nums[1]` and so we have:\n\n```cpp\n// now: left = 1, right = 24\n|4 - 1| // on its left\n|4 - 6| + |4 - 8| + |4 - 10| // on the right\n```\n\nWe apply the formula and see that on the left we have the number of all the elements on the left of our currently parsed number (so, just `1` element on its left now) multiplied the current number `nums[1]` itself (ie: `4`) minus the sum of all the elements on its left (`left == 1`, plus, specularly, the sum of all the elements on its right (`right == 6 + 8 + 10 == 24`) multiplied the number of elements on its right (`len - i - 1 == 5 - 1 - 1 == 3`).\n\nWe update `left` again to be now `+= nums[1]`, thus `left == 5` and we move on like that until the end.\n\nSo, now our base formula is then `i * nums[i] - left + right - (len - i - 1) * nums[i]`, which can be grouped as `right - left - (len - 2 * i - 1) * nums[i]`, or just `right - left - (len - (i << 1) - 1) * nums[i]` if you want to show off you know bitwise operators (not forgetting parentheses, since they have lower priority).\n\nOnce we are done, we can return `res` and we are done :)\n\nThe code:\n\n```cpp\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        // support variables\n        int len = nums.size(), left = 0, right = accumulate(begin(nums), end(nums), 0);\n        vector<int> res(len);\n        // main loop\n        for (int i = 0; i < len; i ++) {\n            // updating right to "lose" the current value\n            right -= nums[i];\n            // updating res with the all the left and right differences\n            res[i] = right - left - (len - (i << 1) - 1) * nums[i];\n            // updating left to "lose" the current value\n            left += nums[i];\n        }\n        return res;\n    }\n};\n```\n\nNot sure why it is said to run so slowly - I can only guess more tests were added later.
['Math', 'C', 'C++']