Problem statement

https://leetcode.com/problems/find-pivot-index/

Solution

We can use cumulative sums (adding zero in the beginning) and check all possible options.

Complexity

It is O(n) for time and O(n) for space.

Code

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