Given two arrays **A[]** and **B[]** consisting of **N** integers, the task is to update array **A[]** by assigning every array element **A[i]** to a single element **B[j]** and update **A[i]** to **A[i] + B[j]** or **A[i] * B[j]**, such that the product of the array **A[]** is maximized.

**Note:** Every array element in both the arrays can be paired with a single element from the other array only once.

**Examples:**

Input:A[] = {1, 1, 6}, B[] = {1, 2, 3}Output:108Explanation:

- Update A[0] = A[0] + B[0], A[] modifies to {2, 1, 6}
- Update A[1] = A[1] + B[1], A[] modifies to {2, 3, 6}
- Update A[0] = A[0] * B[2], A[] modifies to {6, 3, 6}
Therefore, the product of the array A[] is 6 * 3 * 6 = 108.

Input:A[] = {1, 1, 10}, B[] ={1, 1, 1}Output:60Explanation:

- Update A[0] = A[0] + B[0], A[] modifies to {2, 1, 10}
- Update A[1] = A[1] + B[1], A[] modifies to {2, 2, 10}
- Update A[0] = A[0] * B[2], A[] modifies to {3, 2, 10}

**Approach:**The above problem can be solved by using a priority queue(min-heap). Follow the steps below to solve the problem:

- Sort the array
**B[]**. - Insert all elements of array
**A[]**into priority queue in order to get minimum elements each time. - Traverse the given array
**B[]**using variable**j**and popped an element from the priority queue as the maximum of**minE + B[j]**or**minE*B[j]**and push this maximum into the priority queue. - After the above steps, the product of elements in the priority queue is the required result.

Below is the implementation of the above approach :

## Java

`// Java program for the above approach ` ` ` `import` `java.io.*; ` `import` `java.util.*; ` `class` `GFG { ` ` ` ` ` `// Function to find the largest ` ` ` `// product of array A[] ` ` ` `public` `static` `int` `largeProduct( ` ` ` `int` `A[], ` `int` `B[], ` `int` `N) ` ` ` `{ ` ` ` `// Base Case ` ` ` `if` `(N == ` `0` `) ` ` ` `return` `0` `; ` ` ` ` ` `// Store all the elements of ` ` ` `// the array A[] ` ` ` `PriorityQueue<Integer> pq ` ` ` `= ` `new` `PriorityQueue<>(); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `pq.add(A[i]); ` ` ` ` ` `// Sort the Array B[] ` ` ` `Arrays.sort(B); ` ` ` ` ` `// Traverse the array B[] ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) { ` ` ` ` ` `// Pop minimum element ` ` ` `int` `minn = pq.poll(); ` ` ` ` ` `// Check which operation is ` ` ` `// producing maximum element ` ` ` `int` `maximized_element ` ` ` `= Math.max(minn * B[i], ` ` ` `minn + B[i]); ` ` ` ` ` `// Insert resultant element ` ` ` `// into the priority queue ` ` ` `pq.add(maximized_element); ` ` ` `} ` ` ` ` ` `// Evaluate the product ` ` ` `// of the elements of A[] ` ` ` `int` `max_product = ` `1` `; ` ` ` `while` `(pq.size() > ` `0` `) { ` ` ` ` ` `max_product *= pq.poll(); ` ` ` `} ` ` ` ` ` `// Return the maximum product ` ` ` `return` `max_product; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Given arrays ` ` ` `int` `A[] = { ` `1` `, ` `1` `, ` `10` `}; ` ` ` `int` `B[] = { ` `1` `, ` `1` `, ` `1` `}; ` ` ` ` ` `int` `N = ` `3` `; ` ` ` ` ` `// Function Call ` ` ` `System.out.println( ` ` ` `largeProduct(A, B, N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

60

**Time Complexity:** O(N log N)**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Maximum Subarray Sum possible by replacing an Array element by its Square
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Maximize length of increasing subsequence possible by replacing array element by nearest primes
- Maximize product of subarray sum with its minimum element
- Queries to print count of distinct array elements after replacing element at index P by a given element
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Maximize Array sum by swapping at most K elements with another array
- Maximize the number of indices such that element is greater than element to its left
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Replacing an element makes array elements consecutive
- Count ways to split array into two equal sum subarrays by replacing each array element to 0 once
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.