Maximize product obtained by taking one element from each array of a given list | C # 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 ; Given list of N arrays ; Store the maximum positive and minimum negative product possible ; Print the maximum product
using System ; using System . Collections . Generic ; using System . Linq ; public class GFG { static int findProduct ( int number_1 , int number_2 ) { if ( number_1 == Int32 . MinValue number_2 == Int32 . MinValue ) { return 0 ; } else return number_1 * number_2 ; } static List < int > calculateProduct ( List < List < int > > List , int index ) { int highest = List [ index ] . Max ( ) ; int lowest = List [ index ] . Min ( ) ; if ( index + 1 == List . Count ) { if ( lowest < 0 && highest >= 0 ) { return ( new List < int > ( ) { highest , lowest } ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( new List < int > ( ) { Int32 . MinValue , lowest } ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( new List < int > ( ) { highest , Int32 . MinValue } ) ; } } List < int > temp = calculateProduct ( List , index + 1 ) ; int positive = temp [ 0 ] ; int negative = temp [ 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 List < int > ( ) { Math . Max ( highPos , lowNeg ) , Math . Min ( highNeg , lowPos ) } ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( new List < int > ( ) { lowNeg , lowPos } ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( new List < int > ( ) { Math . Max ( lowPos , highPos ) , Math . Min ( lowNeg , highNeg ) } ) ; } return ( new List < int > ( ) { 0 , 0 } ) ; } static public void Main ( ) { List < List < int > > arr = new List < List < int > > ( ) ; arr . Add ( new List < int > ( ) { - 3 , - 4 } ) ; arr . Add ( new List < int > ( ) { 1 , 2 , - 3 } ) ; List < int > ans = calculateProduct ( arr , 0 ) ; Console . WriteLine ( ans [ 0 ] ) ; } }