Maximum Possible Array Sum after Performing given Operations


In this problem, we will perform the given operations on the array elements and find the maximum sum at last.

Here, in each operation, we can select at most X[p] elements from the array and replace them with the Y[p] elements to maximize the sum.

In the naïve approach, we will find X[p] array elements, which are smaller than the Y[p] elements, and replace them with Y[p].

In the efficient approach, we will use the priority queue to get the maximum sum.

Problem statement − We have given nums[] array containing the N numbers. Also, we have given the X[] and Y[] arrays containing the M integers. We need to perform the below operations on the nums[] array.

  • We need to perform the M operations for each element of the X[] and Y[] elements. In each operation, we need to select the maximum X[p] element from the array nums[] and replace it with the Y[p].

The given task is to find the maximum sum of nums[] array elements after performing the M operations.

Sample examples

Input

nums[] = {10, 8, 7, 60, 20, 18, 30, 60}; m = 3; x[] = {1, 2, 5}; y[] = {500, 10, 2};

Output

708

Explanation − Let’s perform each operation one by one.

  • In the first operation, we will replace 7 elements with 500. So, the array becomes {10, 8, 500, 60, 20, 18, 30, 60}.

  • In the second operation, we can replace at most 2 elements with 10, but we have only 1 element less than 10. So, we will replace 8 with 10, and the array becomes {10, 10, 500, 60, 20, 18, 30, 60}.

  • In the third operation, we can replace at most 5 elements with 2, but the array doesn’t contain any element less than 2. So, we won’t replace any elements.

Input

nums[] = {30, 40, 50, 50, 60}; m = 3; x[] = {2, 3, 6}; y[] = {10, 8, 21};

Output

230

Explanation − All elements of the y[] array are less than the original array elements. So, we don’t need to replace any element of the given array to get the maximum sum.

Input

nums[] = {30, 40, 50, 50, 60}; m = 3; x[] = {2, 4, 5}; y[] = {50, 60, 100};

Output

500

Explanation − Here, we can replace the at most x[p] elements in each operation. In the last operation, we can replace each element of the array with 100, and we can get the maximum sum equal to 100.

Approach 1

We will traverse through the x[] and y[] array in this approach. In each iteration, we will sort the array to get the at most x[p] array elements that are smallest and lower than the y[p] elements and replace them with y[p].

Algorithm

Step 1 − Initialize the ‘maxSum’ with 0 to store the maximum sum of array elements.

Step 2 − Start traversing the x[] and y[] array elements.

Step 3 − Take the x[p] value into the temp variable and sort the nums[] array.

Step 4 − Start traversing the sorted array inside the loop.

Step 5 − If the temp is greater than 0, and nums[q] are less than y[p], update the nums[q] with y[p] and decrement the temp value by 1.

Step 6 − Outside the loop, start traversing the updated array, take a sum of all array elements, and store it into the maxSum variable.

Step 7 − Return maxSum, at the end of the function.

Example

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

int getMaxSum(int nums[], int n, int q, int x[], int y[]) {
    int maxSum = 0;
    // Traverse X[] and Y[] array
    for (int p = 0; p < q; p++) {
        // Replacing x[p] number of elements of nums[] array with y[p] if they are lesser than y[p]
        int temp = x[p];
        sort(nums, nums + n);
        for (int q = 0; q < n; q++) {
            if (temp > 0 && nums[q] < y[p]) {
                nums[q] = y[p];
                temp--;
            }
        }
    }
    // Sum of the array
    for (int p = 0; p < n; p++) {
        maxSum += nums[p];
    }
    return maxSum;
}
int main() {
    int nums[] = {10, 8, 7, 60, 20, 18, 30, 60};
    int n = (sizeof nums) / (sizeof nums[0]);
    int m = 3;
    int x[] = {1, 2, 5};
    int y[] = {500, 10, 2};
    cout << "The maximum sum we can get by replacing the array values is " << getMaxSum(nums, n, m, x, y);
    return 0;
}

Output

The maximum sum we can get by replacing the array values is 708

Time complexity − O(M*NlogN), where O(M) to traverse all queries, and O(NlogN) sorts the array in each operation.

Space complexity − O(N) for sorting the array.

Approach 2

In this approach, we will use the priority queue to store the pair of an array element and the number of occurrences of it.

For example, we will push the {nums[p], 1} pair to the priority queue for each array element. Also, we will push the {y[p], x[p]} pairs to the priority queue. In the priority queue, pairs will be sorted according to the first element. So, we can take the highest N elements from the queue. Here, for the pair {y[p], x[p]}, we can take the y[p] elements for x[p] times, and we need to take the total N elements to maximize the sum.

Algorithm

Step 1 − Initialize the ‘maxSum’ with 0 and the priority queue to store the pair of elements and their number of occurrences.

Step 2 − For all array elements, insert the {nums[p], 1} pair into the queue.

Step 3 − After that, insert the {y[p], x[p]} pair into the priority queue.

Step 4 − Make iterations until n is greater than 0.

Step 4.1 − Take the first element from the priority queue.

Step 4.2 − Add the first_ele * max(n, second_ele) into the sum. Here, we use the max(n, second_ele) to handle the last case.

Step 4.3 − Subtract the second_ele from the n.

Step 5 − Return maxSum.

Example

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

int getMaxSum(int nums[], int n, int m, int x[], int y[]) {
    int maxSum = 0, p;
    // To get maximum sum
    priority_queue<pair<int, int>> p_que;
    // Insert nums[] array pairs into the queue
    for (p = 0; p < n; p++)
        p_que.push({nums[p], 1});
    // Push replacement pairs
    for (p = 0; p < m; p++)
        p_que.push({y[p], x[p]});
    // Add the first N elements of the priority queue in the sum
    while (n > 0) {
        // Get top element of priority queue
        auto temp = p_que.top();
        // Remove top element
        p_que.pop();
        // Add value to the sum
        maxSum += temp.first * min(n, temp.second);
        // Change N
        n -= temp.second;
    }
    return maxSum;
}
int main() {
    int nums[] = {10, 8, 7, 60, 20, 18, 30, 60};
    int n = (sizeof nums) / (sizeof nums[0]);
    int m = 3;
    int x[] = {1, 2, 5};
    int y[] = {500, 10, 2};
    cout << "The maximum sum we can get by replacing the array values is " << getMaxSum(nums, n, m, x, y);
    return 0;
}

Output

The maximum sum we can get by replacing the array values is 708

Time complexity − O(N*logN + m*logm), where O(N) and O(m) is for traversing the given array and O(logN) to insert and remove elements from the queue.

Space complexity − O(N+M), to store pairs into the queue.

In the first approach, we need to sort the array in each iteration to find the minimum x[p] elements. Using the priority queue automatically sorts the elements when we insert or remove elements from it as it uses the heap data structure. So it improves the performance of the code.

Updated on: 02-Aug-2023

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements