C++ ✅ | Easy Solution 🔥| Using Prefix, Postfix Sum
Intuition\n Describe your first thoughts on how to solve this problem. \nThe question asks us to find the return another array where at every index is the sum o
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe question asks us to find the return another array where at every index is the sum of absolute difference of every element with ith element.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- For every element at ith place,the sum the absolute difference of left sum minus the ith element multiplied by total number of elements on the left side and right sum and ith element multiplied by total numebr of elements on the right side.\n- We first store the left sum in an vector named leftSum, and right sum in a vector named rightSum.\n- And then in the result vector at every ith place we set the value to the summition of the leftsum and right sum minus ith element multiplied by the elements on the right and left.\n- Return the result vector.\n\n# Complexity\n- Time complexity: O(n) - Iterating through 3 arrays sets the time complexity to O(n).\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n) - The code taks linear space depending on the input size seting the space complexity to O(n).\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 n=nums.size();\n        vector<int> result(n);\n        vector<int> leftSum(n);\n        vector<int> rightSum(n);\n\n        leftSum[0]=nums[0];\n        for(int i=1; i<n; i++){\n            leftSum[i]=nums[i]+leftSum[i-1];\n        }\n\n        rightSum[n-1]=nums[n-1];\n        for(int i=n-2; i>=0; i--){\n            rightSum[i]=nums[i]+rightSum[i+1];\n        }\n\n        for(int i=0; i<n; i++){\n            int sum=((nums[i]*i)-leftSum[i]+rightSum[i]-(nums[i]*(n-i-1)));\n            result[i]=sum;\n        }\n\n        return result;\n    }\n};\n```
['C++']