Find maximum product sub-array
Detailed guide and Python implementation for the 'Find maximum product sub-array' problem.
Problem Statement
Write a function max_product_subarray(arr) that takes a list of integers arr and returns the maximum product of any contiguous subarray. A subarray must contain at least one element.
- •1 <= len(arr) <= 10^4
- •-10 <= arr[i] <= 10
Examples
arr = [2, 3, -2, 4]
6
The subarray [2, 3] has the maximum product: 2 * 3 = 6.
arr = [-2, 0, -1]
0
The subarray [0] gives the maximum product of 0, since all other products are negative or zero.
arr = [-2, -3, 4]
24
The entire array: (-2) * (-3) * 4 = 24? Wait, that's the subarray [-2,-3,4] = 24. Yes, two negatives multiply to positive.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Find the Length of a List in Python
Learn how to find the length of a list in Python using the len() function. Understand the O(1) time complexity and checking counts.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.