maximum subarray whose sum equals 0
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Maximum Subarray with Sum Equals 0
The problem of finding a maximum subarray with a sum of zero is a fascinating problem in computer science, especially in algorithm design and analysis. This article delves into the technicalities of this problem and provides a comprehensive guide on how to approach it.
Problem Statement
Given an array of integers, the objective is to find the largest contiguous subarray whose elements sum up to zero and to return its length. A subarray is defined as a contiguous part of an array.
Technical Explanation
To solve this problem efficiently, we use a hash-based technique, which offers an average time complexity of O(n). The basic idea is to use prefix sums and a hash map to store each unique prefix sum we encounter, along with the earliest index at which it occurs.
Steps to Solve:
- Initialization:
- Initialize a hash map to store prefix sums as keys and their respective indices as values.
- Set the initial prefix sum to zero and store it in the hash map with the index
-1. - Initialize variables for the maximum length of subarray with sum zero (
max_len) and the current prefix sum (curr_sum).
- Iterate through the Array:
- For each element in the array, add it to the
curr_sum. - Check if
curr_sumis present in the hash map:- If yes, calculate the length of the subarray using the hash map and update
max_lenif this length is greater. - If no, add
curr_sumto the hash map with its index.
- Result:
- The value stored in
max_lenat the end will be the length of the maximum subarray with sum 0.
Example
Consider the array [1, 2, -3, 3]
.
- Prefix Sum Calculation:
- After the first element (1):
curr_sum = 1 - After the second element (2):
curr_sum = 3 - After the third element (-3):
curr_sum = 0curr_sum= 0 exists with initial index (-1).- Subarray found from index 0 to 2, length = 3.
- After the fourth element (3):
curr_sum = 3curr_sum= 3 exists at index 1.- Subarray found from index 2 to 3, length = 2.
- Final Answer: Subarray length = 3, as [1, 2, -3] sums to zero.
Code Implementation
Here is a Python implementation of the above logic:
- All Elements Zero: Entire array is the subarray.
- No Zero-Sum Subarray: The output would be 0 if all prefix sums are distinct.
- Different Target Sums: The algorithm can be extended to find maximum subarrays with sums equal to any given target.
- Two-dimensional Cases: Similar algorithms can be adapted to find submatrices in 2D arrays with a sum of zero.
- Multi-query Versions: Preprocessing techniques can help answer multiple queries about zero-sum subarrays efficiently.
Related reading
- Maximum sum of non consecutive elements
- Maximum Sum SubArray
- Maximum sum sublist?
- maximum sum subrectangle in a sparse matrix
- Maximum weighted bipartite matching, constraint ordering of each graph is preserved
- Maximum xor among all subsets of an array
- MD5 vs CRC32 Which one's better for common use?
- Meaning of the ⊥ symbol in distributed algorithm context

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.