Problem statement

https://leetcode.com/problems/find-the-middle-index-in-array/

Solution

Just use accumulate here.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def findMiddleIndex(self, nums):
        acc = [0] + list(accumulate(nums))
        n = len(nums)
        for i in range(n):
            if acc[i] == acc[-1] - acc[i+1]: return i
        return -1