Skip to content
LC-1991 Easy LeetCode

1991. Find the Middle Index in Array

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 68% Topics: Array, Prefix Sum
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def findMiddleIndex(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        total = sum(nums)
        accu = 0
        for i, x in enumerate(nums):
            if accu*2 == total-x:
                return i
            accu += x
        return -1

Solution from kamyu104/LeetCode-Solutions · MIT