Maximize product of same | Python3 implementation to maximize product of same - indexed elements of same size subsequences ; Utility function to find the maximum ; Utility function to find the maximum scalar product from the equal length sub - sequences taken from the two given array ; Return a very small number if index is invalid ; If the sub - problem is already evaluated , then return it ; Take the maximum of all the recursive cases ; Function to find maximum scalar product from same size sub - sequences taken from the two given array ; Initialize a 2 - D array for memoization ; Driver Code
INF = 10000000 NEW_LINE def maximum ( A , B , C , D ) : NEW_LINE INDENT return max ( max ( A , B ) , max ( C , D ) ) NEW_LINE DEDENT def maxProductUtil ( X , Y , A , B , dp ) : NEW_LINE INDENT if ( X < 0 or Y < 0 ) : NEW_LINE INDENT return - INF NEW_LINE DEDENT if ( dp [ X ] [ Y ] != - 1 ) : NEW_LINE INDENT return dp [ X ] [ Y ] NEW_LINE DEDENT dp [ X ] [ Y ] = maximum ( A [ X ] * B [ Y ] + maxProductUtil ( X - 1 , Y - 1 , A , B , dp ) , A [ X ] * B [ Y ] , maxProductUtil ( X - 1 , Y , A , B , dp ) , maxProductUtil ( X , Y - 1 , A , B , dp ) ) NEW_LINE return dp [ X ] [ Y ] NEW_LINE DEDENT def maxProduct ( A , N , B , M ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( m ) ] for i in range ( n ) ] NEW_LINE return maxProductUtil ( N - 1 , M - 1 , A , B , dp ) NEW_LINE DEDENT a = [ - 2 , 6 , - 2 , - 5 ] NEW_LINE b = [ - 3 , 4 , - 2 , 8 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE print ( maxProduct ( a , n , b , m ) ) NEW_LINE