Maximize product obtained by taking one element from each array of a given list | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findProduct ( int number_1 , int number_2 ) { if ( number_1 == INT_MIN or number_2 == INT_MIN ) return 0 ; else return number_1 * number_2 ; } pair < int , int > calculateProduct ( vector < vector < int > > List , int index ) { int highest = * max_element ( List [ index ] . begin ( ) , List [ index ] . end ( ) ) ; int lowest = * min_element ( List [ index ] . begin ( ) , List [ index ] . end ( ) ) ; if ( index + 1 == List . size ( ) ) { if ( lowest < 0 and highest > = 0 ) return { highest , lowest } ; else if ( lowest <= 0 and highest <= 0 ) return { INT_MIN , lowest } ; else if ( lowest >= 0 and highest >= 0 ) return { highest , INT_MIN } ; } pair < int , int > temp = calculateProduct ( List , index + 1 ) ; int positive = temp . first ; int negative = temp . second ; int highPos = findProduct ( highest , positive ) ; int highNeg = findProduct ( highest , negative ) ; int lowPos = findProduct ( lowest , positive ) ; int lowNeg = findProduct ( lowest , negative ) ; if ( lowest < 0 and highest > = 0 ) return { max ( highPos , lowNeg ) , min ( highNeg , lowPos ) } ; else if ( lowest <= 0 and highest <= 0 ) return { lowNeg , lowPos } ; else if ( lowest >= 0 and highest >= 0 ) return { max ( lowPos , highPos ) , min ( lowNeg , highNeg ) } ; } int main ( ) { int N = 2 ; vector < vector < int > > arr { { -3 , -4 } , { 1 , 2 , -3 } } ; pair < int , int > ans = calculateProduct ( arr , 0 ) ; cout << ans . first << endl ; }