🤯Optimized-Without-Extra-Space.py😎
\n# Complexity\n- Time complexity:\n O(n) \n- Space complexity:\n O(n) --> O(1)\nJust use one variable for Prefix sum instead of an arr\nSuffix sum = Sum - Pref
\n# Complexity\n- Time complexity:\n $$O(n)$$ \n- Space complexity:\n $$O(n)$$ --> $$O(1)$$\n`Just use one variable for Prefix sum instead of an arr`\n`Suffix sum = Sum - PrefixSum`\n# Code\n```js\nclass Solution:\n    def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:\n        pr=[None]*len(nums)\n        pr[0]=nums[0]\n        for i in range(1,len(nums)):pr[i]=pr[i-1]+nums[i]\n        su=[None]*len(nums)\n        su[len(nums)-1]=nums[len(nums)-1]\n        for i in range(len(nums)-2,-1,-1):su[i]=su[i+1]+nums[i]\n        return [(nums[i]*(i+1)-pr[i])+su[i]-nums[i]*(len(nums)-i) for i in range(len(nums))]\n```\n```py\nclass Solution:\n    def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:\n        sm,pr=sum(nums),0\n        for i in range(len(nums)):\n            pr+=nums[i]\n            nums[i]=(((i+1)*nums[i]-pr)+(sm-pr)-(len(nums)-i-1)*nums[i])\n        return nums\n```\n\n![](https://cdn.hashnode.com/res/hashnode/image/upload/v1599902056795/y4dkWJHP9.jpeg?auto=compress,format&format=webp)
['Python3']