Skip to content

Latest commit

 

History

History
 
 

max_product_subarray

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product.

For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6.

Solution

Iterate through the array, maintain a running max (positive product) and a running min (negative product).

If element is zero, clear previous product