Maximize product of array by replacing array elements with its sum or product with element from another array
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: 108
Explanation:
- 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: 60
Explanation:
- 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 :
C++
#include <bits/stdc++.h>
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);
}
|
Java
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));
}
}
|
Python3
def largeProduct(A, B, N):
if (N = = 0 ):
return 0
pq = []
for i in range (N):
pq.append(A[i])
B.sort()
pq.sort(reverse = True )
for i in range (N):
minn = pq.pop()
maximized_element = max (minn * B[i], minn + B[i])
pq.append(maximized_element)
pq.sort(reverse = True )
max_product = 1
while ( len (pq) > 0 ):
max_product * = pq.pop();
return max_product
A = [ 1 , 1 , 10 ]
B = [ 1 , 1 , 1 ]
N = 3
print (largeProduct(A, B, N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int largeProduct( int [] A, int [] B, int N)
{
if (N == 0)
{
return 0;
}
List< int > pq = new List< int >();
for ( int i = 0; i < N; i++)
{
pq.Add(A[i]);
}
Array.Sort(B);
pq.Sort();
for ( int i = 0; i < N; i++)
{
int min = pq[0];
pq.RemoveAt(0);
int maximized_element = Math.Max(min* B[i], min + B[i]);
pq.Add(maximized_element);
pq.Sort();
}
int max_product = 1;
while (pq.Count > 0)
{
max_product *= pq[0];
pq.RemoveAt(0);
}
return max_product;
}
static public void Main ()
{
int [] A = { 1, 1, 10 };
int [] B = { 1, 1, 1 };
int N = 3;
Console.WriteLine(largeProduct(A, B, N));
}
}
|
Javascript
<script>
function largeProduct(A, B, N)
{
if (N == 0)
return 0;
let pq=[];
for (let i = 0; i < N; i++)
pq.push(A[i]);
pq.sort( function (a,b){ return a-b;});
B.sort( function (a,b){ return a-b;});
for (let i = 0; i < N; i++) {
let minn = pq.shift();
let maximized_element
= Math.max(minn * B[i],
minn + B[i]);
pq.push(maximized_element);
pq.sort( function (a,b){ return a-b;});
}
let max_product = 1;
while (pq.length > 0) {
max_product *= pq.shift();
}
return max_product;
}
let A=[1, 1, 10 ];
let B=[1, 1, 1];
let N = 3;
document.write(largeProduct(A, B, N));
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Last Updated :
22 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...