Largest Ratio Contiguous subarray | Function to return maximum of two double values ; Check if a is greater than b then return a ; Function that returns the Ratio of max Ratio subarray ; Variable to store the maximum ratio ; Compute the product while traversing for subarrays ; Calculate the ratio ; Update max ratio ; Print the answer ; Driver code
function maximum ( a , b ) { if ( a > b ) return a ; return b ; } function maxSubarrayRatio ( arr , n ) { var maxRatio = - 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i ; j < n ; j ++ ) { var ratio = arr [ i ] ; for ( var k = i + 1 ; k <= j ; k ++ ) { ratio = ratio / arr [ k ] ; } maxRatio = maximum ( maxRatio , ratio ) ; } } return maxRatio ; } var arr = [ 2 , 2 , 4 , - 0.2 , - 1 ] ; var n = arr . length ; document . write ( maxSubarrayRatio ( arr , n ) ) ;