FunctionRacerProblemsRace

Longest Zero-Sum Stretch

MediumHash mapsPython, JavaScript, TypeScript, Java, C++

Return the length of the longest contiguous stretch of nums whose items add up to exactly 0.

If no non-empty stretch adds up to 0 (including when nums is empty), return 0.

Example 1

nums [1,-1,3,-3,2]
returns 4

1, -1, 3, -3 adds up to 0 and has length 4; no longer stretch does.

Example 2

nums [2,3,-5,1,0]
returns 3

2, 3, -5 is the longest stretch with sum 0, length 3. The single 0 is a stretch of length 1.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Hash maps problems