Problem statement

https://binarysearch.com/problems/Longest-Zero-Sublist-Sum/

Solution

Almost the same as Leetcode 0525. Contiguous Array.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, nums):
        ind = defaultdict(list)
        for i, x in enumerate([0] + list(accumulate(nums))):
            ind[x] += [i]

        max_len = 0
        for i in ind:
            max_len = max(max_len, ind[i][-1]-ind[i][0])
        return max_len