Maximize product of array by replacing array elements with its sum or product with element from another array | C ++ program for the above approach ; Function to find the largest product of array A [ ] ; Base Case ; Store all the elements of the array A [ ] ; Sort the Array B [ ] ; Traverse the array B [ ] ; Pop minimum element ; Check which operation is producing maximum element ; Insert resultant element into the priority queue ; Evaluate the product of the elements of A [ ] ; Return the maximum product ; Driver Code ; Given arrays ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largeProduct ( vector < int > A , vector < int > B , int N ) { if ( N == 0 ) return 0 ; priority_queue < int , vector < int > , greater < int > > pq ; for ( int i = 0 ; i < N ; i ++ ) pq . push ( A [ i ] ) ; sort ( B . begin ( ) , B . end ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { int minn = pq . top ( ) ; pq . pop ( ) ; int maximized_element = max ( minn * B [ i ] , minn + B [ i ] ) ; pq . push ( maximized_element ) ; } int max_product = 1 ; while ( pq . size ( ) > 0 ) { max_product *= pq . top ( ) ; pq . pop ( ) ; } return max_product ; } int main ( ) { vector < int > A = { 1 , 1 , 10 } ; vector < int > B = { 1 , 1 , 1 } ; int N = 3 ; cout << largeProduct ( A , B , N ) ; }