O(N) time O(1) space solution (C++)
The intution is the following:\nFirst calculate total_sum with one pass through the array\nFor each element in position i (0 indexed):\n\tsum of the absolute di
The intution is the following:\nFirst calculate total_sum with one pass through the array\nFor each element in position i (0 indexed):\n\tsum of the absolute difference to the left = i*nums[i] - left_prefix_sum \n    sum of the absolute difference to the right = (total_sum - left_prefix_sum - nums[i]) - nums[i]) * (n-i-1) = (total_sum - left_prefix_sum) - (n-i)nums[i]\n\t\nAll this can be done in-place traversing from the left in the second pass.\n\t\n\tvector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        int total_sum = 0;\n        int left_prefix_sum = 0;\n        int n = nums.size();\n        \n        for (int i = 0; i < n; i++)\n            total_sum += nums[i];\n        \n        \n        for (int i = 0; i < n; i++) {\n            int val = (i*nums[i] - left_prefix_sum)  + ((total_sum - left_prefix_sum) - (n-i)*nums[i]);\n            left_prefix_sum += nums[i];\n            nums[i] = val;\n        }\n        \n        return nums;\n    }\n
[]