Maximize product of array by replacing array elements with its sum or product with element from another array
Last Updated :
22 Jun, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the largest
// product of array A[]
int largeProduct(vector<int> A,
vector<int> B, int N)
{
// Base Case
if (N == 0)
return 0;
// Store all the elements of
// the array A[]
priority_queue<int, vector<int>,
greater<int>> pq;
for(int i = 0; i < N; i++)
pq.push(A[i]);
// Sort the Array B[]
sort(B.begin(), B.end());
// Traverse the array B[]
for(int i = 0; i < N; i++)
{
// Pop minimum element
int minn = pq.top();
pq.pop();
// Check which operation is
// producing maximum element
int maximized_element = max(minn * B[i],
minn + B[i]);
// Insert resultant element
// into the priority queue
pq.push(maximized_element);
}
// Evaluate the product
// of the elements of A[]
int max_product = 1;
while (pq.size() > 0)
{
max_product *= pq.top();
pq.pop();
}
// Return the maximum product
return max_product;
}
// Driver Code
int main()
{
// Given arrays
vector<int> A = { 1, 1, 10 };
vector<int> B = { 1, 1, 1 };
int N = 3;
// Function Call
cout << largeProduct(A, B, N);
}
// This code is contributed by mohit kumar 29
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));
}
}
Python3
# Python program for the above approach
# Function to find the largest
# product of array A[]
def largeProduct(A, B, N):
# Base Case
if(N == 0):
return 0
# Store all the elements of
# the array A[]
pq = []
for i in range(N):
pq.append(A[i])
# Sort the Array B[]
B.sort()
pq.sort(reverse = True)
# Traverse the array B[]
for i in range(N):
# Pop minimum element
minn = pq.pop()
# Check which operation is
# producing maximum element
maximized_element = max(minn * B[i], minn + B[i])
# Insert resultant element
# into the priority queue
pq.append(maximized_element)
pq.sort(reverse = True)
# Evaluate the product
# of the elements of A[]
max_product = 1
while(len(pq) > 0):
max_product *= pq.pop();
# Return the maximum product
return max_product
# Driver Code
# Given arrays
A = [1, 1, 10]
B = [1, 1, 1]
N = 3
# Function Call
print(largeProduct(A, B, N))
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
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[]
List<int> pq = new List<int>();
for(int i = 0; i < N; i++)
{
pq.Add(A[i]);
}
// Sort the Array B[]
Array.Sort(B);
pq.Sort();
// Traverse the array B[]
for(int i = 0; i < N; i++)
{
int min = pq[0];
// Pop minimum element
pq.RemoveAt(0);
// Check which operation is
// producing maximum element
int maximized_element = Math.Max(min* B[i], min + B[i]);
// Insert resultant element
// into the priority queue
pq.Add(maximized_element);
pq.Sort();
}
// Evaluate the product
// of the elements of A[]
int max_product = 1;
while(pq.Count > 0)
{
max_product *= pq[0];
pq.RemoveAt(0);
}
// Return the maximum product
return max_product;
}
// Driver Code
static public void Main ()
{
// Given arrays
int[] A = { 1, 1, 10 };
int[] B = { 1, 1, 1 };
int N = 3;
// Function Call
Console.WriteLine(largeProduct(A, B, N));
}
}
// This code is contributed by rag2127
JavaScript
<script>
// Javascript program for the above approach
// Function to find the largest
// product of array A[]
function largeProduct(A, B, N)
{
// Base Case
if (N == 0)
return 0;
// Store all the elements of
// the array A[]
let pq=[];
for (let i = 0; i < N; i++)
pq.push(A[i]);
pq.sort(function(a,b){return a-b;});
// Sort the Array B[]
B.sort(function(a,b){return a-b;});
// Traverse the array B[]
for (let i = 0; i < N; i++) {
// Pop minimum element
let minn = pq.shift();
// Check which operation is
// producing maximum element
let maximized_element
= Math.max(minn * B[i],
minn + B[i]);
// Insert resultant element
// into the priority queue
pq.push(maximized_element);
pq.sort(function(a,b){return a-b;});
}
// Evaluate the product
// of the elements of A[]
let max_product = 1;
while (pq.length > 0) {
max_product *= pq.shift();
}
// Return the maximum product
return max_product;
}
// Driver Code
let A=[1, 1, 10 ];
let B=[1, 1, 1];
let N = 3;
document.write(largeProduct(A, B, N));
// This code is contributed by patel2127
</script>
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Similar Reads
Maximum sum of Array formed by replacing each element with sum of adjacent elements Given an array arr[] of size N, the task is to find the maximum sum of the Array formed by replacing each element of the original array with the sum of adjacent elements.Examples: Input: arr = [4, 2, 1, 3] Output: 23 Explanation: Replacing each element of the original array with the sum of adjacent
9 min read
Maximum sum of Array formed by replacing each element with sum of adjacent elements Given an array arr[] of size N, the task is to find the maximum sum of the Array formed by replacing each element of the original array with the sum of adjacent elements.Examples: Input: arr = [4, 2, 1, 3] Output: 23 Explanation: Replacing each element of the original array with the sum of adjacent
9 min read
Modify array to another given array by replacing array elements with the sum of the array | Set-2 Given an Array input[] consisting only of 1s initially and an array target[] of size N, the task is to check if the array input[] can be converted to target[] by replacing input[i] with the sum of array elements in each step. Examples: Input: input[] = { 1, 1, 1 }, target[] = { 9, 3, 5 } Output: YES
10 min read
Minimize cost for reducing array by replacing two elements with sum at most K times for any index Given an array arr[] of size N and an integer K. The task is to find the minimum cost required to collect the sum of the array. The sum of the array is collected by picking any element and adding it to an element of any index in the array. The addition of elements at the same index is allowed for at
11 min read
Maximize maximum possible subarray sum of an array by swapping with elements from another array Given two arrays arr[] and brr[] consisting of N and K elements respectively, the task is to find the maximum subarray sum possible from the array arr[] by swapping any element from the array arr[] with any element of the array brr[] any number of times. Examples: Input: N = 5, K = 4, arr[] = { 7, 2
7 min read
Maximize sum of odd-indexed array elements by repeatedly selecting at most 2*M array elements from the beginning Given an array arr[] consisting of N integers and an integer M (initially 1), the task is to find the maximum sum of array elements chosen by Player A when two players A and B plays the game optimally according to the following rules: Player A starts the game.At every chance, X number of elements ca
15+ min read