
Given an array A of N integers, find the length of the longest subarray which sums to zero.
If there is no subarray which sums to zero, return 0.
Input:
A of size NOutput:
Input: A = [1, -1, 3, 2, -2, -8, 1, 7, 10, 23]
Output: 5
Explanation: Subarray [3, 2, -2, -8, 1] has sum 0 and length 5
Input: A = [15, -2, 2, -8, 1, 7, 10, 23]
Output: 0
Explanation: No zero-sum subarray
Input: A = [1, 2, -2, 1, -1]
Output: 4
Explanation: [2, -2, 1, -1] or [1, 2, -2, 1] both have length 4
1 ≤ N ≤ 10^5-10^9 ≤ A[i] ≤ 10^9Store first occurrence of each prefix sum.
When same prefix appears again:
Example:
A = [1, -1, 3, -3]
Index: 0 1 2 3
Prefix: 1 0 3 0
Prefix 0 at indices 1 and 3
Length = 3 - 1 = 2
Subarray [3, -3]
Check all subarrays, calculate sum, track max length.
function longestZeroSumBrute(A) {
let maxLen = 0;
for (let i = 0; i < A.length; i++) {
let sum = 0;
for (let j = i; j < A.length; j++) {
sum += A[j];
if (sum === 0) {
maxLen = Math.max(maxLen, j - i + 1);
}
}
}
return maxLen;
}
A = [1, -1, 3, -3]
i=0: [1,-1]=0(len=2), [1,-1,3,-3]=0(len=4)
i=1: [-1,1]=0? No
i=2: [3,-3]=0(len=2)
Max length: 4
Time: O(N²)
Space: O(1)
A = [1, -1, 3, -3]
Check subarrays:
[1,-1] = 0, len=2
[1,-1,3,-3] = 0, len=4 ← Maximum
Result: 4
Use Prefix Sum + Hash Map:
Store first occurrence index of each prefix sum.
When prefix repeats:
1. Initialize: prefixSum=0, map={0: -1}, maxLen=0
2. For each index i:
a. Add A[i] to prefixSum
b. If prefixSum in map:
len = i - map[prefixSum]
maxLen = max(maxLen, len)
c. Else: map[prefixSum] = i
3. Return maxLen
function longestZeroSumSubarray(A) {
const map = new Map();
let prefixSum = 0;
let maxLen = 0;
// Initialize for subarrays starting from index 0
map.set(0, -1);
for (let i = 0; i < A.length; i++) {
prefixSum += A[i];
if (map.has(prefixSum)) {
// Found same prefix sum before
const length = i - map.get(prefixSum);
maxLen = Math.max(maxLen, length);
} else {
// Store first occurrence
map.set(prefixSum, i);
}
}
return maxLen;
}
// Test cases
console.log(longestZeroSumSubarray([1, -1, 3, 2, -2, -8, 1, 7, 10, 23])); // 5
console.log(longestZeroSumSubarray([15, -2, 2, -8, 1, 7, 10, 23])); // 0
console.log(longestZeroSumSubarray([1, 2, -2, 1, -1])); // 4
A = [1, -1, 3, -3]
Initialize: prefixSum=0, map={0:-1}, maxLen=0
i=0, A[0]=1:
prefixSum = 1
Not in map → map = {0:-1, 1:0}
i=1, A[1]=-1:
prefixSum = 0
Found at index -1 → len = 1-(-1) = 2
maxLen = 2
i=2, A[2]=3:
prefixSum = 3
Not in map → map = {0:-1, 1:0, 3:2}
i=3, A[3]=-3:
prefixSum = 0
Found at index -1 → len = 3-(-1) = 4
maxLen = 4
Result: 4
Time: O(N)
Space: O(N)
A = [1, -1, 3, -3]
Index: 0 1 2 3
Prefix: 1 0 3 0
↑ ↑
Same prefix!
Length = 3 - (-1) = 4
Subarray [1,-1,3,-3]
// No zero-sum
longestZeroSumSubarray([1, 2, 3]); // 0
// Entire array
longestZeroSumSubarray([1, -1]); // 2
// Single zero
longestZeroSumSubarray([0]); // 1
// Multiple zeros
longestZeroSumSubarray([0, 0, 0]); // 3
✅ First Occurrence: Store index, not count
✅ Length Calculation: current - first
✅ O(N) Solution: Single pass
Happy Coding! 🚀

I'm Rahul, Sr. Software Engineer (SDE II) and passionate content creator. Sharing my expertise in software development to assist learners.
More about me