java TC  : O(n) |SC : O(n) | clear explanation
Basic Approach\nfor every element, the absolute difference is \n\nlet length = nums.length\n(nums[i] - nums[0]) +(nums[i] - nums[1]) +....... + (nums[i] - nums[
# **Basic Approach**\nfor every element, the absolute difference is \n```\nlet length = nums.length\n(nums[i] - nums[0]) +(nums[i] - nums[1]) +....... + (nums[i] - nums[i-1]) + (nums[i] - nums[i]) + (nums[i+1] - nums[i]) +........... + (nums[length-2] - nums[i]) + (nums[length-1] - nums[i]) \n\n(nums[i] - nums[0]) +(nums[i] - nums[1]) +....... + (nums[i] - nums[i-1]) + 0 + (nums[i+1] - nums[i]) +........... + (nums[length-2] - nums[i]) + (nums[length-1] - nums[i]) \n\n(nums[i] * i  - (nums[0] + nums[1] + ... nums[i-1]) ) + ( (nums[i+1] + nums[i+2] + .... nums[length-1]) - nums[i]*(length - 1 -i))\n\n(nums[i]*i - prefixsum[i]) + (suffixsum[i] - nums[i]*(length - 1 -i))\n\n\n```\n\n# **Code**\nclass Solution {\n\n    public int[] getSumAbsoluteDifferences(int[] nums) {\n        int prefix[] = new int[nums.length];\n        int suffix[] = new int[nums.length];\n        for(int i = 1; i < nums.length; i++) {\n            prefix[i] = prefix[i-1] + nums[i-1];\n        }\n        for(int j = nums.length-2; j >= 0; j--) {\n            suffix[j] = suffix[j+1] + nums[j+1];\n        }\n        for(int i = 0; i < nums.length; i++) {\n            prefix[i] = (i*nums[i] - prefix[i]) + (suffix[i] - (nums.length-1-i)*nums[i]);\n        }\n        return prefix;\n        \n    }\n\n}
['Prefix Sum', 'Java']