Concept Flow - Maximum Product Subarray
Start at index 0
Initialize max_prod, min_prod, result
For each element i in array
If element < 0, swap max_prod and min_prod
Update max_prod = max(element, max_prod * element)
Update min_prod = min(element, min_prod * element)
Update result = max(result, max_prod)
Move to next element
Back to For loop
Return result
↩Back to For loop
We scan the array once, keeping track of the maximum and minimum products ending at each position, swapping them when a negative number appears, and update the result with the maximum product found.
