[
array
accumulate
]
BinarySearch 0884 Index with Equal Left and Right Sums
Problem statement
https://binarysearch.com/problems/Index-with-Equal-Left-and-Right-Sums/
Solution
Equal to Leetcode 1991. Find the Middle Index in Array.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def solve(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