Maximum Product Subarray | Returns the product of max product subarray . ; Initializing result ; traversing in current subarray ; updating result every time to keep an eye over the maximum product ; updating the result for ( n - 1 ) th index . ; Driver code
function maxSubarrayProduct ( arr , n ) { let result = arr [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) { let mul = arr [ i ] ; for ( let j = i + 1 ; j < n ; j ++ ) { result = Math . max ( result , mul ) ; mul *= arr [ j ] ; } result = Math . max ( result , mul ) ; } return result ; } let arr = [ 1 , - 2 , - 3 , 0 , 7 , - 8 , - 2 ] ; let n = arr . length ; document . write ( " " + maxSubarrayProduct ( arr , n ) ) ;