🔥Python3🔥 Prefix sum
Algorithm\n(1) To get the average of something, we first need to get the sum.\n(2) At each index i, if we know the sum of the first i + 1 elements, and the sum 
**Algorithm**\n(1) To get the average of something, we first need to get the sum.\n(2) At each index ```i```, if we know the sum of the first i + 1 elements, and the sum of the last n - i - 1 elements, we can calculate their averages.\n - We can compute the sum of the first i + 1 elements by adding the value at ```i``` to the sum of the first ```i``` elements (prefix sum).\n - We can get the sum of the last n - i - 1 elements by using the total sum of the entire array - the sum of the first i + 1 elements\n\n(3) At each ```i```, we just need to store ```i``` if the current ```absolute difference``` is less than any ```absolute difference``` we have seen before.\n**TC: O(n)**\n\n```python\nclass Solution:\n    def minimumAverageDifference(self, nums: List[int]) -> int:\n        # leftSum represents the sum of the first i + 1 elements at index i\n        n, totalSum, leftSum = len(nums), sum(nums), 0\n        \n        # res[0] is the minimum absolute difference, and res[1] is the index.\n        res = [inf,inf]\n        \n        for i,v in enumerate(nums):\n            leftSum += v\n            \n            leftAvg = leftSum//(i+1)\n            # when we are at the last index, the right hand side sum is 0. (To aviod divide by zero error)\n            rightAvg = (totalSum-leftSum)//(n-i-1) if n-i-1!=0 else 0\n            absDif = abs(leftAvg-rightAvg)\n            \n            # min will take care of "If there are multiple such indices, return the smallest one."\n            res = min(res,[absDif,i])\n        \n        # return the index\n        return res[1]\n```\n\n**Upvote** if you like this post.\n\n**Connect with me on [LinkedIn](https://www.linkedin.com/in/meida-chen-938a265b/)** if you\'d like to discuss other related topics\n\n\uD83C\uDF1F If you are interested in **Machine Learning** || **Deep Learning** || **Computer Vision** || **Computer Graphics** related projects and topics, check out my **[YouTube Channel](https://www.youtube.com/@meidachen8489)**! Subscribe for regular updates and be part of our growing community.
[]