Simple well explained C++ solution || TC:O(n), SC:O(1)
Intuition\nHere we are first calculating the total sum of the array. Then, for each element, it calculates the sum of absolute differences with all other elemen
# Intuition\nHere we are first calculating the total sum of the array. Then, for each element, it calculates the sum of absolute differences with all other elements by separately considering the elements to its left and right. The results are stored in a vector and returned. This approach allows the problem to be solved in linear time complexity.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe approach for this code is as follows:\n\n1. **Initialization**: Initialize `total_sum` to 0, `n` to the size of the input vector `nums`, and `curr_sum` to 0. `total_sum` is used to store the sum of all elements in `nums`, and `curr_sum` is used to store the cumulative sum of elements as we iterate through `nums`.\n\n2. **Calculate Total Sum**: Iterate through `nums` and add each element to `total_sum`.\n\n3. **Calculate Sum of Absolute Differences**: For each element `nums[i]`, calculate the sum of absolute differences as follows:\n    - Update `curr_sum` by adding `nums[i]`.\n    - Calculate `Rsum` as the difference between `total_sum` and `curr_sum`. This represents the sum of the elements to the right of `nums[i]`.\n    - Calculate `Lsum` as the difference between `curr_sum` and `nums[i]`. This represents the sum of the elements to the left of `nums[i]`.\n    - Update `Lsum` and `Rsum` to represent the sum of absolute differences of `nums[i]` with all elements to its left and right respectively.\n    - Add the sum of `Lsum` and `Rsum` to the answer vector `ans`.\n\n4. **Return Result**: Return `ans`, which is a vector containing the sum of absolute differences for each element in `nums`.\n\nThe key idea here is to calculate the sum of absolute differences for each element in linear time by separately considering the elements to its left and right. This approach significantly improves the efficiency of the code. \n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {\n        int total_sum=0;\n        int n=nums.size();\n        for(int i=0; i<n; i++)total_sum+=nums[i];\n        vector<int> ans;\n        int curr_sum=0;\n        for(int i=0; i<n; i++){\n            curr_sum+=nums[i];\n            int Rsum=total_sum-curr_sum;\n            int Lsum=curr_sum-nums[i];\n            Lsum=(i*nums[i])-Lsum;\n            Rsum=Rsum-(n-(i+1))*nums[i];\n            ans.push_back(Lsum+Rsum);\n        }\n        return ans;\n    }\n};\n```
['C++']