Maximize product obtained by taking one element from each array of a given list | Java program for the above approach ; 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
import java . io . * ; import java . util . * ; class GFG { static int findProduct ( int number_1 , int number_2 ) { if ( number_1 == Integer . MIN_VALUE number_2 == Integer . MIN_VALUE ) { return 0 ; } else return number_1 * number_2 ; } static ArrayList < Integer > calculateProduct ( ArrayList < ArrayList < Integer > > List , int index ) { int highest = Collections . max ( List . get ( index ) ) ; int lowest = Collections . min ( List . get ( index ) ) ; if ( index + 1 == List . size ( ) ) { if ( lowest < 0 && highest >= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( highest , lowest ) ) ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( Integer . MIN_VALUE , lowest ) ) ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( highest , Integer . MIN_VALUE ) ) ) ; } } ArrayList < Integer > temp = calculateProduct ( List , index + 1 ) ; int positive = temp . get ( 0 ) ; int negative = temp . get ( 1 ) ; int highPos = findProduct ( highest , positive ) ; int highNeg = findProduct ( highest , negative ) ; int lowPos = findProduct ( lowest , positive ) ; int lowNeg = findProduct ( lowest , negative ) ; if ( lowest < 0 && highest >= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( Math . max ( highPos , lowNeg ) , Math . min ( highNeg , lowPos ) ) ) ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( lowNeg , lowPos ) ) ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( Math . max ( lowPos , highPos ) , Math . min ( lowNeg , highNeg ) ) ) ) ; } return ( new ArrayList < Integer > ( Arrays . asList ( 0 , 0 ) ) ) ; } public static void main ( String [ ] args ) { int N = 2 ; ArrayList < ArrayList < Integer > > arr = new ArrayList < ArrayList < Integer > > ( ) ; arr . add ( new ArrayList < Integer > ( Arrays . asList ( - 3 , - 4 ) ) ) ; arr . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 2 , - 3 ) ) ) ; ArrayList < Integer > ans = calculateProduct ( arr , 0 ) ; System . out . println ( ans . get ( 0 ) ) ; } }