C++ || O(N) Solution || prefix sum
Logic\n\n---\n at an index i what do we want in our resultant array at index i is simply.....\n---\n\n   *  ans[i] =_( (nums[i]-nums[0]) +(nums[i]-nums[1]) + ..
**Logic**\n\n---\n* **at an index `i` what do we want in our resultant array at index `i` is simply.....**\n---\n\n   *  **`ans[i]`** =**_`( (nums[i]-nums[0]) +(nums[i]-nums[1]) + ...... + (nums[i] -nums[i-1]) )`** **+** **`( (nums[i+1]-nums[i]) + (nums[i+2]-nums[i]) + ... + (nums[n-1] -nums[i]) )`_**\n   \n   ---\n   *  **if you try to observe above expression then you will get to know that ...**\n --- \n   *  **`ans[i]`** =**_`( nums[i]*(number of elements before element i ) -(nums[0] +nums[1]+...+nums[i-1) +(nums[i+1]+nums[i+2]+....+nums[n-1]) -nums[i]*(number of elements after index i))`_**\n   \n ----\n \n **Upvote if my solution and my explanation helps you :)**\n ```C++\n class Solution\n{\npublic:\n    vector<int> getSumAbsoluteDifferences(vector<int> &nums)\n    {\n        vector<int> ans;\n        //  making prefix and suffix array\n        vector<int> prefix, suffix;\n\n        // running sum\n        int preSum = 0;\n\n        int n = nums.size();\n\n        // taking total sum\n        int total = 0;\n        for (auto x : nums)\n            total += x;\n\n        // building prefix and suffix array\n        for (int i = 0; i < n; i++)\n        {\n            preSum += nums[i];\n            // prefix array contains total left sum + nums[i]\n            prefix.push_back(preSum);\n            // suffix array contains total right sum from index i\n            suffix.push_back(total - preSum);\n        }\n        // building absolute sum array\n        for (int i = 0; i < n; i++)\n        {\n            int val = nums[i] * (i + 1) - prefix[i] + suffix[i] - nums[i] * (n - i - 1);\n            ans.push_back(val);\n        }\n        return ans;\n    }\n};\n```\n
['C']