Maximize product of array by replacing array elements with its sum or product with element from another array | Java 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
import java . io . * ; import java . util . * ; class GFG { public static int largeProduct ( int A [ ] , int B [ ] , int N ) { if ( N == 0 ) return 0 ; PriorityQueue < Integer > pq = new PriorityQueue < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) pq . add ( A [ i ] ) ; Arrays . sort ( B ) ; for ( int i = 0 ; i < N ; i ++ ) { int minn = pq . poll ( ) ; int maximized_element = Math . max ( minn * B [ i ] , minn + B [ i ] ) ; pq . add ( maximized_element ) ; } int max_product = 1 ; while ( pq . size ( ) > 0 ) { max_product *= pq . poll ( ) ; } return max_product ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 1 , 10 } ; int B [ ] = { 1 , 1 , 1 } ; int N = 3 ; System . out . println ( largeProduct ( A , B , N ) ) ; } }