Python O(N) 2 pass solution with explanation!
This problem requires a simple observation\nInput: nums = [2,3,5]\nresult[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,\nresult[1] = |3-2| + |3-3| + |3-5| = 1 + 0
**This problem requires a simple observation**\nInput: nums = [2,3,5]\nresult[0] = **|2-2|** + |2-3| + |2-5| = 0 + 1 + 3 = 4,\nresult[1] = |3-2| + **|3-3|** + |3-5| = 1 + 0 + 2 = 3,\nresult[2] = |5-2| + |5-3| + **|5-5|** = 3 + 2 + 0 = 5.\n\n\nSo lets see result[i] will have ith sum element as 0. Everything before that element is in descending order. \nEx - 3+2+0\nand everything after will be in ascending order. So we basically just need to see how much the result gains prior to the 0 occuring in result[i-1] -  \n\nas |2-2|=0 but |3-2| =1 so result[1] gains 1 point,\n\n**after the zero** -\n\n|2-3|=1, |3-3|=0 so result[1] looses 1 point,\n|2-5| =3, |3-5|=2 so result[1] looses 1 more point.\n\nhence result[1]= result[0]+1-1-1=3\n\nSeeing this observation will always be true it is a non decreasing sequence we can derive this formula according to element i.\n\ntemp = result[0]\nt=nums[i-1]\ntemp= temp - (len(nums)-i)*(nums[i]-t)+(i)*(nums[i]-t)\n\nImplementing this in a loop and voila we get the answer!!!\n\n```\nclass Solution:\n    def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:\n        out = []\n        a = nums[0]\n        temp = 0\n        for i in range(len(nums)):\n            temp+=abs(a-nums[i])\n        out.append(temp)\n        for i in range(1,len(nums)):\n            t=nums[i-1]\n            temp= temp - (len(nums)-i)*(nums[i]-t)+(i)*(nums[i]-t)\n            out.append(temp)\n        return out
['Python', 'Python3']