Maximum and maximum zero-sum subarray problems
Maximum subarray problem
The maximum subarray problem consists in finding the maximum contiguous subarray into a longer sequence of numbers. Given the following sequence of values
\[V = \{ 1, 2, -4, 3, 6, -4, -1, 0, 4, 2, -4, 6 \}\]the maximum subarray is {\( 3, 6, -4, -1, 0, 4, 2, -4, 6 \)} since it grants a grand-total of 12.
Kadane’s algorithm is \( O(n) \) and allows for a quick identification of the maximum subarray
Maximum zero-sum subarray problem
The maximum zero-sum subarray problem is a modified statement from the previous problem: it asks to find the largest subarray whose elements add up to zero. The maximum zero-sum subarray for the sequence
\[V = \{ 1, 2, -4, 3, 6, -4, -1, 0, 4, 2, -4, 6 \}\]is {\( -4, 3, 6, -4, -1, 0 \)}. As usual a naive \( O(n^2) \) algorithm is available by exploring all the subintervals \( [i;j] \), anyway a \( O(n) \) algorithm based on sum hashing would be a better match. The algorithm keeps a running sum and stores intermediate sums into a hash table. In the following cases, a potential maximum is found
- If no maximum zero-sum subarray has been identified yet and there’s a 0 element, the maximum zero-sum array is trivially composed of the 0 number itself
- If the running sum reaches zero from the beginning of the array, the longest zero-sum subarray consists of the entire array till the index we reached
- If a previous intermediate sum is found, it means that between the previous sum \( X \) and the current sum \( X \) there must have been an interval of values which sum to zero. Check for the maximum between the current maximum length and the length of this intermediate interval