Maximize product of array by replacing array elements with its sum or product with element from another array


We are given two arrays of the same length and we have to apply some operations to get the product of the first array with all elements maximum. The operations are to multiply or add any element of the second array only once to any element of the first array only once. We can add or multiply two different elements from the second array element to a single first array element. After all operations, we have to take the product of all the elements of the first array and return that.

Example

Let's understand the problem with the help of an example −

Input

int arr[] = {2, 1, 3};
int brr[] = {3, 2, 4};

Output

216

Explanation

First we will add 2 for the second array to 1 from the first array and it will become 3, then we will multiply 3 with 2 and it will become 6. At last, we will multiply 4 by 3 and it will become 12. So final array will be 6, 12, and 3.

Approach

First, let us see the priority queue data structure −

The priority queue is a heap-based data structure defined in the C++ STL. It has two versions in which it stores either the greatest or smallest element on the top. We are using the version that will store the smallest element on the top and for that syntax is −

priority_queue<data_type, vector<data_type>, greater<data_type>> priority_queue_name;

Here data_type is the type of data that we need to store in the priority queue such as the int, long long, float, vector, etc., and priority_queue_name is the name that we give to the priority queue.

Let us move the steps required for implementation

  • First, we need to create a function that will take both given arrays and their length as the parameter and return an integer value that is the answer.

  • In the function, first, we will create a priority queue, and using for loop we will add all the elements of the first array to it.

  • After that, we will sort the second array and then traverse it.

  • At each iteration, we are going to get the top element of the priority queue which will be the smallest element from the given first array.

  • Our goal is to get the smallest element and do operations on the smallest element to make it greater because our target is to make the smallest element as maximum as possible.

  • We will move over the array and at each iteration we will pop the smallest element out and after applying an operation on it which is multiplication or addition based on which will produce a larger result, and add it to the priority queue.

  • In the end, we will move the priority queue and take the product of all the elements and return it to the main function, where we will print it.

Example

#include <bits/stdc++.h>
using namespace std;

// function to get the result 
int getProduct(int arr[], int brr[], int n){
   // create a priority queue. It will store the elements in decreasing order 
   priority_queue<int, vector<int>, greater<int>> pq;    
   // traversing over the first array to add all the elements to the priority queue 
   for(int i=0; i<n; i++){
      pq.push(arr[i]);
   }    
   // sorting the second array 
   sort(brr, brr + n);    
   // traversing over the second array to multiply its elements 
   for(int i=0; i<n; i++){
      int cur = pq.top(); // smallest element of first array
      // pop it out 
      pq.pop();        
      // apply operation on it 
      cur = max(cur * brr[i], cur + brr[i]);
      // again add it to queue 
      pq.push(cur);
   }    
   // traverse over the  priority queue to get the answer 
   int ans = 1; // variable to store the answer    
   for(int i=0; i<n; i++){
      ans *= pq.top();
      pq.pop();
   }    
   // return final answer
   return ans;
}
int main(){
   // defining the given arrays 
   int arr[] = {2, 1, 3};
   int brr[] = {3, 2, 4};    
   int n = 3; // size of the given arrays     
   cout<<"The maximum product of the array elements after operations is: "<<getProduct(arr, brr, n)<<endl;
   return 0; 
}

Output

The maximum product of the array elements after operations is: 216

Time and Space Complexity

The time complexity of the above code is O(N*log(N)), where N is the size of the given array and we are getting a logarithmic factor due to the priority queue.

The space complexity of the above code is O(N), as we are using extra space in the form of the priority queue.

Conclusion

In this tutorial, we have implemented a program to find the maximum product of the given array by applying some given operations on it. We were choosing some elements from the second given array and multiplying or adding them with the elements of the first array. We have implemented a program using a priority queue in O(N*log(N)) time and O(N) extra space.

Updated on: 24-Aug-2023

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements