Maximize product obtained by taking one element from each array of a given list | Function to return the product of 2 numbers ; If any of the two numbers is None ; Otherwise , return the product ; Function to calculate maximum product by taking only one element from each array present in the list ; Find the maximum and minimum present in the current array ; If last index is reached , then return the highest ( positive ) and lowest ( negative ) values ; Store the positive and negative products returned by calculating for the remaining arrays ; Store highest positive product ; Store product of highest with negative ; Store product of lowest with positive ; Store lowest negative product ; Return the maximum positive and minimum negative product ; Count of given arrays ; Given list of N arrays ; Store the maximum positive and minimum negative product possible ; Print the maximum product
def findProduct ( number_1 , number_2 ) : NEW_LINE INDENT if ( number_1 == None or number_2 == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return number_1 * number_2 NEW_LINE DEDENT DEDENT def calculateProduct ( List , index ) : NEW_LINE INDENT highest = max ( List [ index ] ) NEW_LINE lowest = min ( List [ index ] ) NEW_LINE if ( index + 1 == len ( List ) ) : NEW_LINE INDENT if ( lowest < 0 and highest >= 0 ) : NEW_LINE INDENT return [ highest , lowest ] NEW_LINE DEDENT elif ( lowest <= 0 and highest <= 0 ) : NEW_LINE INDENT return [ None , lowest ] NEW_LINE DEDENT elif ( lowest >= 0 and highest >= 0 ) : NEW_LINE INDENT return [ highest , None ] NEW_LINE DEDENT DEDENT [ positive , negative ] = calculateProduct ( List , index + 1 ) NEW_LINE highPos = findProduct ( highest , positive ) NEW_LINE highNeg = findProduct ( highest , negative ) NEW_LINE lowPos = findProduct ( lowest , positive ) NEW_LINE lowNeg = findProduct ( lowest , negative ) NEW_LINE if ( lowest < 0 and highest >= 0 ) : NEW_LINE INDENT return [ max ( highPos , lowNeg ) , min ( highNeg , lowPos ) ] NEW_LINE DEDENT elif ( lowest <= 0 and highest <= 0 ) : NEW_LINE INDENT return [ lowNeg , lowPos ] NEW_LINE DEDENT elif ( lowest >= 0 and highest >= 0 ) : NEW_LINE INDENT return [ max ( lowPos , highPos ) , min ( lowNeg , highNeg ) ] NEW_LINE DEDENT DEDENT N = 2 NEW_LINE arr = [ [ - 3 , - 4 ] , [ 1 , 2 , - 3 ] ] NEW_LINE positive , negative = calculateProduct ( arr , 0 ) NEW_LINE print ( positive ) NEW_LINE