766. Largest Subarray with Sum 0

You are given an integer array arr of size n which contains both positive and negative integers. Your task is to find the length of the longest contiguous subarray with sum equal to 0.

Return the length of such a subarray. If no such subarray exists, return 0.

Example 1:

Input: arr = [15, -2, 2, -8, 1, 7, 10, 23]

Output: 5

Explanation:

The subarray [-2, 2, -8, 1, 7] sums up to 0 and has the maximum length among all such subarrays.

Example 2:

Input: arr = [2, 10, 4]

Output: 0

Explanation:

There is no subarray whose elements sum to 0.

Now Your Turn!

Pick the correct output for the given input

Input: arr = [1, 0, -4, 3, 1, 0]

Still unsure what the problem is asking ?

Let鈥檚 go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= arr.length <= 106
  • -103 <= arr[i] <= 103 for each valid index i

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int maxLen(vector<int>& arr) {
// Your code goes here
}
};
Test Case

Input:

Arr