Zero-sum at the start
Input
arr = [1, -1, 3, -3, 5]
Output
4
Prefix sum becomes 0 at index 3, so subarray from index 0 to 3 has sum 0 and length 4.
Full lesson preview
Find the length of the longest contiguous subarray whose elements sum to zero using prefix sums and a map.
Problem statement
Task
Examples
Input
arr = [1, -1, 3, -3, 5]
Output
4
Prefix sum becomes 0 at index 3, so subarray from index 0 to 3 has sum 0 and length 4.
Input format
Output format
Constraints
Samples
Input
arr = [1, 2, -3, 3]
Output
3
Subarray [1,2,-3] sums to 0 and has length 3.