Largest Ratio Contiguous subarray | Python3 program for the above approach ; 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
import sys NEW_LINE def maximum ( a , b ) : NEW_LINE INDENT if ( a > b ) : NEW_LINE INDENT return a NEW_LINE DEDENT return b NEW_LINE DEDENT def maxSubarrayRatio ( arr , n ) : NEW_LINE INDENT maxRatio = - sys . maxsize - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT ratio = arr [ i ] NEW_LINE for k in range ( i + 1 , j + 1 ) : NEW_LINE INDENT ratio = ratio // arr [ k ] NEW_LINE DEDENT maxRatio = maximum ( maxRatio , ratio ) NEW_LINE DEDENT DEDENT return int ( maxRatio ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 2 , 4 , - 0.2 , - 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxSubarrayRatio ( arr , n ) ) NEW_LINE DEDENT