Maximum Subarray Product is a classic dynamic programming problem similar to the well-known Maximum Subarray Sum problem solved by Kadane’s Algorithm. In this problem, we are tasked with finding the contiguous subarray within an integer array that produces the maximum product. Unlike maximum sum, products behave differently because multiplication with negatives and zeros alters the results drastically. This is where the Modified Kadane’s Algorithm comes into play.
In this article, we will cover:
- Understanding the Maximum Subarray Product problem
- Why the original Kadane’s algorithm fails here
- The Modified Kadane’s Algorithm: step-by-step
- Illustrations with diagrams
- Python implementation with examples
- Complexity analysis
Understanding the Problem
Given an array of integers (positive, negative, and zeros), we need to find the contiguous subarray that yields the maximum product. For example:
Input: [2, 3, -2, 4]
Output: 6
Explanation: [2, 3] gives product = 6
Input: [-2, 0, -1]
Output: 0
Explanation: Single element [0] gives maximum product.
Why Kadane’s Algorithm for Sum Fails Here
Kadane’s Algorithm works efficiently for sums because negative numbers always reduce the sum. However, in products:
- A negative multiplied by another negative gives a positive.
- A zero resets the product completely.
- The smallest negative product so far might become the largest when multiplied by a negative number later.
Thus, we must track both the maximum product so far and the minimum product so far at every index.
The Modified Kadane’s Algorithm
- Initialize:
max_so_far = nums[0]min_so_far = nums[0]result = nums[0]
- Iterate through the array starting from index 1.
- If the current element is negative, swap
max_so_farwithmin_so_far. - Update:
max_so_far = max(nums[i], nums[i] * max_so_far)min_so_far = min(nums[i], nums[i] * min_so_far)
- Update the result as
result = max(result, max_so_far).
Step-by-Step Example
Consider the array:
nums = [2, 3, -2, 4]
| Index | Element | max_so_far | min_so_far | Result |
|---|---|---|---|---|
| 0 | 2 | 2 | 2 | 2 |
| 1 | 3 | 6 | 3 | 6 |
| 2 | -2 | -2 | -12 | 6 |
| 3 | 4 | 4 | -48 | 6 |
Answer = 6
Python Implementation
def max_product_subarray(nums):
max_so_far = nums[0]
min_so_far = nums[0]
result = nums[0]
for i in range(1, len(nums)):
if nums[i] < 0:
max_so_far, min_so_far = min_so_far, max_so_far
max_so_far = max(nums[i], nums[i] * max_so_far)
min_so_far = min(nums[i], nums[i] * min_so_far)
result = max(result, max_so_far)
return result
# Example usage:
print(max_product_subarray([2, 3, -2, 4])) # Output: 6
print(max_product_subarray([-2, 0, -1])) # Output: 0
print(max_product_subarray([-2, 3, -4])) # Output: 24
Visual Representation of Changing Products
Complexity Analysis
- Time Complexity: O(n), because we loop through the array once.
- Space Complexity: O(1), since only a few extra variables are used.
Interactive Exploration
Try modifying the input array inside the Python function to experiment with different cases. Look for:
- Effect of zeros resetting products
- Role of negative values turning into large positives when multiplied
- Cases where the result is just a single element
Conclusion
The Maximum Subarray Product problem shows why handling multiplication is more complex than sums. The Modified Kadane’s Algorithm elegantly tracks both maximum and minimum products, ensuring negative numbers are handled correctly. This algorithm runs in linear time and constant space, making it efficient for real applications such as financial data, physics simulations, and machine learning preprocessing.
By combining theory, step-by-step demonstration, diagrams, and working Python code, this guide equips you to master the maximum product subarray problem confidently.








