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 ; Driver Code Count of given arrays ; Given list of N arrays ; Store the maximum positive and minimum negative product possible ; Print the maximum product
function findProduct ( number_1 , number_2 ) { if ( number_1 == Number . MIN_VALUE number_2 == Number . MIN_VALUE ) { return 0 ; } else return number_1 * number_2 ; } function calculateProduct ( List , index ) { let highest = Math . max ( ... List [ index ] ) ; let lowest = Math . min ( ... List [ index ] ) ; if ( index + 1 == List . length ) { if ( lowest < 0 && highest >= 0 ) { return ( [ highest , lowest ] ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( [ Number . MIN_VALUE , lowest ] ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( [ highest , Number . MIN_VALUE ] ) ; } } let temp = calculateProduct ( List , index + 1 ) ; let positive = temp [ 0 ] ; let negative = temp [ 1 ] ; let highPos = findProduct ( highest , positive ) ; let highNeg = findProduct ( highest , negative ) ; let lowPos = findProduct ( lowest , positive ) ; let lowNeg = findProduct ( lowest , negative ) ; if ( lowest < 0 && highest >= 0 ) { return ( [ Math . max ( highPos , lowNeg ) , Math . min ( highNeg , lowPos ) ] ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( [ lowNeg , lowPos ] ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( [ Math . max ( lowPos , highPos ) , Math . min ( lowNeg , highNeg ) ] ) ; } return ( [ 0 , 0 ] ) ; } let N = 2 ; let arr = [ [ - 3 , - 4 ] , [ 1 , 2 , - 3 ] ] ; let ans = calculateProduct ( arr , 0 ) ; document . write ( ans [ 0 ] ) ;