Maximize array sum by replacing at most L elements to R for Q queries
Given an array arr[] consisting of N integers and an array Query[][] consisting of M pairs of the type {L, R}, the task is to find the maximum sum of the array by performing the queries Query[][] such that for each query {L, R} replace at most L array elements to the value R.
Examples:
Input: arr[]= {5, 1, 4}, Query[][] = {{2, 3}, {1, 5}}
Output: 14
Explanation:
Following are the operations performed:
Query 1: For the Query {2, 3}, do nothing.
Query 2: For the Query {1, 5}, replace at most L(= 1) array element with value R(= 5), replace arr[1] with value 5.
After the above steps, array modifies to {5, 5, 4}. The sum of array element is 14, which is maximum.
Input: arr[] = {1, 2, 3, 4}, Query[][] = {{3, 1}, {2, 5}}
Output: 17
Approach: The given problem can be solved with the help of the Greedy Approach. The main idea to maximize the array sum is to perform the query to increase the minimum number to a maximum value as the order of the operations does not matter as they are independent of each other. Follow the steps below to solve the given problem:
- Maintain a min-heap priority queue and store all the array elements.
- Traverse the given array of queries Q[][] and for each query {L, R} perform the following steps:
- Change the value of at most L elements smaller than R to the value R, starting from the smallest.
- Perform the above operation, pop the elements smaller than R and push R at their places in the priority queue.
- After completing the above steps, print the sum of values stored in the priority queue as the maximum sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maximumArraySumWithMQuery(
int arr[], vector<vector< int > >& Q,
int N, int M)
{
priority_queue< int , vector< int >,
greater< int > >
pq;
for ( int i = 0; i < N; i++) {
pq.push(arr[i]);
}
for ( int i = 0; i < M; i++) {
int l = Q[i][0];
int r = Q[i][1];
for ( int j = 0; j < l; j++) {
if (pq.top() < r) {
pq.pop();
pq.push(r);
}
else {
break ;
}
}
}
int ans = 0;
while (!pq.empty()) {
ans += pq.top();
pq.pop();
}
cout << ans;
}
int main()
{
int N = 3, M = 2;
int arr[] = { 5, 1, 4 };
vector<vector< int > > Query
= { { 2, 3 }, { 1, 5 } };
maximumArraySumWithMQuery(
arr, Query, N, M);
return 0;
}
|
Java
import java.util.PriorityQueue;
class GFG {
public static void maximumArraySumWithMQuery( int arr[], int [][] Q, int N, int M) {
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for ( int i = 0 ; i < N; i++) {
pq.add(arr[i]);
}
for ( int i = 0 ; i < M; i++) {
int l = Q[i][ 0 ];
int r = Q[i][ 1 ];
for ( int j = 0 ; j < l; j++) {
if (pq.peek() < r) {
pq.remove();
pq.add(r);
}
else {
break ;
}
}
}
int ans = 0 ;
while (!pq.isEmpty()) {
ans += pq.peek();
pq.remove();
}
System.out.println(ans);
}
public static void main(String args[]) {
int N = 3 , M = 2 ;
int arr[] = { 5 , 1 , 4 };
int [][] Query = { { 2 , 3 }, { 1 , 5 } };
maximumArraySumWithMQuery(arr, Query, N, M);
}
}
|
Python3
from queue import PriorityQueue
def maximumArraySumWithMQuery(arr, Q, N, M):
pq = PriorityQueue()
for i in range (N):
pq.put(arr[i])
for i in range (M):
l = Q[i][ 0 ];
r = Q[i][ 1 ];
for j in range (l):
if (pq.queue[ 0 ] < r):
pq.get();
pq.put(r);
else :
break
ans = 0 ;
while ( not pq.empty() ):
ans + = pq.queue[ 0 ];
pq.get();
print (ans)
N = 3
M = 2
arr = [ 5 , 1 , 4 ]
Query = [[ 2 , 3 ], [ 1 , 5 ]]
maximumArraySumWithMQuery(arr, Query, N, M)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void maximumArraySumWithMQuery( int [] arr,
int [, ] Q,
int N,
int M)
{
List< int > pq = new List< int >();
for ( int i = 0; i < N; i++) {
pq.Add(arr[i]);
}
pq.Sort();
for ( int i = 0; i < M; i++) {
int l = Q[i, 0];
int r = Q[i, 1];
for ( int j = 0; j < l; j++) {
if (pq[0] < r) {
pq.Remove(pq[0]);
pq.Add(r);
pq.Sort();
}
else {
break ;
}
}
}
int ans = 0;
while (pq.Count > 0) {
ans += pq[0];
pq.Remove(pq[0]);
}
Console.WriteLine(ans);
}
public static void Main( string [] args)
{
int N = 3, M = 2;
int [] arr = { 5, 1, 4 };
int [, ] Query = { { 2, 3 }, { 1, 5 } };
maximumArraySumWithMQuery(arr, Query, N, M);
}
}
|
Javascript
function maximumArraySumWithMQuery(arr, Q, N, M) {
let pq = [];
for (let i = 0; i < N; i++) {
pq.push(arr[i]);
}
for (let i = 0; i < M; i++) {
let l = Q[i][0];
let r = Q[i][1];
for (let j = 0; j < l; j++) {
if (pq[0] < r) {
pq.shift();
pq.push(r);
pq.sort((a, b) => a - b);
}
else {
break ;
}
}
}
let ans = 1;
while (pq.length > 0) {
ans += pq.shift() + 1;
}
console.log(ans);
}
let N = 3, M = 2;
let arr = [5, 1, 4];
let Query = [[2, 3], [1, 5]];
maximumArraySumWithMQuery(arr, Query, N, M);
|
Time Complexity: O(M*N*log N)
Auxiliary Space: O(N)
Last Updated :
15 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...