Processing three type of queries
Last Updated :
23 Aug, 2023
Given Q queries in array A[][2] of three types of the form {type, X} and an empty array B[], the task for this problem is to process these queries in order.
- Type 1: Number X will be given insert that element into array B[].
- Type 2: Number X will be given add that number to all elements present in array B[].
- Type 3: Erase the largest element of array B[] and print it.
Examples:
Input: A[][2] = {{1, 3}, {1, 5}, {3, -1}, {2, 2}, {3, -1}}
Output: 5 5
Explanation: Queries are processed in following order:
- Query – {1, 3}: This is query of type 1 it says insert element 3 in array B[].
- Query – {1, 5}: This is query of type 1 it says insert element 5 in array B[].
- Query – {3, -1}: This is query of type 3. The array B[] now contains a element of value 3 and another with 5. Pick up the ball with the largest of them that is 5 delete it from array B[].
- Query – {2, 2}: The array B[] now contains just a element with value 3. Replace this integer with 3 + 2 = 5.
- Query – {3, -1}: The array B[] now contains just a element with value 5. delete this element, record 5.
Therefore, print 5 and 5, in the order they are recorded.
Input: A[][2] = {{1, 1000}, {2, 1000}, {2, 1000}, {2, 1000}, {2, 1000}, {3, -1}}
Output: 5000
Approach: To solve the problem follow the below idea:
Priority Queue can be used to solve this problem.
Below are the steps for the above approach:
- Create a priority queue.
- Create variable netInc = 0 that will keep track of the increase in elements of the array by query 2.
- Iterate through all queries from 0 to N – 1.
- If the type of query is 1 then insert that element in the priority queue after subtracting it from netInc.
- If the type of query is 2 then increase the value of netInc by the respective amount.
- If the type of query is 3 then print the largest element of the priority queue after adding it with netInc and then delete that element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void processQueries( int A[][2], int N)
{
multiset< int > ms;
int netInc = 0;
for ( int i = 0; i < N; i++) {
if (A[i][0] == 1) {
ms.insert(A[i][1] - netInc);
}
else if (A[i][0] == 2) {
netInc += A[i][1];
}
else {
cout << *(--ms.end()) + netInc << " " ;
ms.erase(--ms.end());
}
}
cout << endl;
}
int32_t main()
{
int N = 5;
int A[][2] = {
{ 1, 3 }, { 1, 5 }, { 3, -1 }, { 2, 2 }, { 3, -1 }
};
processQueries(A, N);
int N1 = 6;
int A1[][2] = { { 1, 1000 }, { 2, 1000 }, { 2, 1000 }, { 2, 1000 }, { 2, 1000 }, { 3, -1 } };
processQueries(A1, N1);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void processQueries( int [][] A, int N) {
TreeSet<Integer> ms = new TreeSet<>();
int netInc = 0 ;
for ( int i = 0 ; i < N; i++) {
if (A[i][ 0 ] == 1 ) {
ms.add(A[i][ 1 ] - netInc);
}
else if (A[i][ 0 ] == 2 ) {
netInc += A[i][ 1 ];
}
else {
System.out.print(ms.last() + netInc + " " );
ms.remove(ms.last());
}
}
System.out.println();
}
public static void main(String[] args) {
int N = 5 ;
int [][] A = {
{ 1 , 3 }, { 1 , 5 }, { 3 , - 1 }, { 2 , 2 }, { 3 , - 1 }
};
processQueries(A, N);
int N1 = 6 ;
int [][] A1 = {
{ 1 , 1000 }, { 2 , 1000 }, { 2 , 1000 }, { 2 , 1000 }, { 2 , 1000 }, { 3 , - 1 }
};
processQueries(A1, N1);
}
}
|
Python3
from sortedcontainers import SortedList
def processQueries(A, N):
ms = SortedList()
netInc = 0
for i in range (N):
if A[i][ 0 ] = = 1 :
ms.add(A[i][ 1 ] - netInc)
elif A[i][ 0 ] = = 2 :
netInc + = A[i][ 1 ]
else :
print (ms[ - 1 ] + netInc, end = " " )
ms.remove(ms[ - 1 ])
print ()
if __name__ = = '__main__' :
N = 5
A = [
[ 1 , 3 ], [ 1 , 5 ], [ 3 , - 1 ], [ 2 , 2 ], [ 3 , - 1 ]
]
processQueries(A, N)
N1 = 6
A1 = [
[ 1 , 1000 ], [ 2 , 1000 ], [ 2 , 1000 ], [ 2 , 1000 ], [ 2 , 1000 ], [ 3 , - 1 ]
]
processQueries(A1, N1)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void ProcessQueries( int [, ] A, int N)
{
SortedSet< int > ms = new SortedSet< int >();
int netInc = 0;
for ( int i = 0; i < N; i++) {
if (A[i, 0] == 1) {
ms.Add(A[i, 1] - netInc);
}
else if (A[i, 0] == 2) {
netInc += A[i, 1];
}
else {
Console.Write(ms.Max + netInc + " " );
ms.Remove(ms.Max);
}
}
Console.WriteLine();
}
public static void Main( string [] args)
{
int N = 5;
int [, ] A = { { 1, 3 },
{ 1, 5 },
{ 3, -1 },
{ 2, 2 },
{ 3, -1 } };
ProcessQueries(A, N);
int N1 = 6;
int [, ] A1
= { { 1, 1000 }, { 2, 1000 }, { 2, 1000 },
{ 2, 1000 }, { 2, 1000 }, { 3, -1 } };
ProcessQueries(A1, N1);
}
}
|
Javascript
function processQueries(A, N) {
let ms = new Set();
let netInc = 0;
for (let i = 0; i < N; i++) {
if (A[i][0] === 1) {
ms.add(A[i][1] - netInc);
}
else if (A[i][0] === 2) {
netInc += A[i][1];
}
else {
let largest = Array.from(ms).reduce((max, val) => val > max ? val : max, -Infinity) + netInc;
console.log(largest + ' ' );
let lastElement = Math.max(...ms);
ms. delete (lastElement);
}
}
console.log( '' );
}
let N = 5;
let A = [
[1, 3], [1, 5], [3, -1], [2, 2], [3, -1]
];
processQueries(A, N);
let N1 = 6;
let A1 = [
[1, 1000], [2, 1000], [2, 1000], [2, 1000], [2, 1000], [3, -1]
];
processQueries(A1, N1);
|
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...