Best Solution Prefix and Suffix Sum S.C O(1) in Java
Intuition\n Describe your first thoughts on how to solve this problem. \n\n\n# Approach\n Describe your approach to solving the problem. \npreffix and suffix su
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\npreffix and suffix sum.\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int[] getSumAbsoluteDifferences(int[] nums) {\n        int n = nums.length , rightSum  = 0,leftSum = 0,totalSum = 0 ;\n        for(int i: nums) rightSum +=i;\n        for(int i = 0 ; i <n ; i++){\n            rightSum -=nums[i];\n            leftSum+=nums[i];\n            totalSum = Math.abs((i+1)*nums[i] -  leftSum) +\n            Math.abs((n-i-1)*nums[i]-rightSum);\n            nums[i] =  totalSum;\n        }\n    return nums;\n    }\n}\n\n```\n\n![c7ed53ec-f37f-4429-b7b2-79fb768a16c3_1678237613.4171617.jpeg](https://assets.leetcode.com/users/images/9db5e3a8-419f-4dd0-84a0-0411c59b3f78_1678460639.2804708.jpeg)
['Prefix Sum', 'Java']