Formula Explained with Neat Code , Beats 100%.
Intuition\n Describe your first thoughts on how to solve this problem. \n     PreFixSum\n\n# Approach\n Describe your approach to solving the problem. \n     Li
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n     PreFixSum\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n     Linear\nIts Obvious that bruteForce approach would lead to TLE , so in such case we have to think of better approach(es) , as we need to subtract x from remaining elements , |x-y|+|x-z|==|2*x-(y+z)| .\nMore formally\n|x-y|+|x-s|+...+|x-a|==|k*x-(y+s+...+a)|.\nWe use this concept to neighbourhood of x so that we get the answer by excluding current element as it doesnt have any significance i.e\n|x-x|==0.\n\nLeftHalf=(i x a[i])-(p[0]-p[i]); [i x a[i] == size of leftHalf]\nRightHalf=((size - i- 1) x a[i]) - p[i]-a[i]) [size-i-1 == size of right half]\n\nAs you can see we have used a term p[i] , it is a prefixSum array\nit store the cumulative sum of elements starting from i=0 till i\'th index. which solves 90% of the problem and which cutoff the redundancy in calculation (Brute Force).\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(2*N)=O(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(2*N)=O(N)\n\n# Code\n```\nclass Solution {\n    public int[] getSumAbsoluteDifferences(int[] arr) {\n        int size=arr.length;\n        int pre_fix[] = new int[size];\n        for(int i=size-1;i>=0;i--){\n            if(i==size-1){\n                pre_fix[i]=arr[i];\n            }\n            else pre_fix[i]=arr[i]+pre_fix[i+1];\n        }\n        int ans[] = new int[size];\n        for(int i=0;i<size;i++){\n           int left=Math.abs(i*arr[i]-(pre_fix[0]-pre_fix[i]));\n           int right=Math.abs((size-i-1)*arr[i]-(pre_fix[i]-arr[i]));\n           ans[i]=left+right;\n        }\n        return ans;\n        \n    }\n}\n```
['Array', 'Math', 'Prefix Sum', 'Java']