Brute Force to Optimal Simple and Easy
Solution - 1: Brute Force - O(n^2) --> TLE\n\nclass Solution {\n    public int[] getSumAbsoluteDifferences(int[] nums) {\n        int n = nums.length;\n        
Solution - 1: Brute Force - O(n^2) --> TLE\n```\nclass Solution {\n    public int[] getSumAbsoluteDifferences(int[] nums) {\n        int n = nums.length;\n        int[] result = new int[n];\n        for(int i = 0; i < n; i++)\n            for(int j = 0; j < n; j++)\n                result[i] += Math.abs(nums[i] - nums[j]);\n        \n        return result;\n    }\n}\n```\n\n********\n\nSolution - 2: Optimal - O(n) --> AC\n\n```\nclass Solution {\n    public int[] getSumAbsoluteDifferences(int[] nums) {\n        int n = nums.length;\n        int[] result = new int[n];\n        int sum = 0;\n        for(int i = 0; i < n; i++)\n            sum += nums[i];\n        \n        int currSum = 0;\n        for(int i = 0; i < n; i++) {\n            // result[i] = (i * nums[i] - currSum) + (sum - currSum - (n - i) * nums[i]); // left part + right part\n            result[i] = sum - 2 * currSum - (n - 2 * i) * nums[i];\n            currSum += nums[i];\n        }\n        \n        return result;\n    }\n}\n```
[]