# Queries to add, remove and return the difference of maximum and minimum in C++

C++Server Side ProgrammingProgramming

In this problem, we are given Q queries. These are of three types, they are −

• Query 1: Add number N to the list.

• Query 2: Remove number N to the list.

• Query 3: Return the difference of minimum and maximum element of the list.

Our task is to create a program to solve queries to add, remove, and return the difference of maximum and minimum in C++.

## Problem Description

We will be given a Q number of queries that we will be performing on the list. There are 3 types of queries to add, remove, and find the difference of maximum and minimum element of the list. Using which we will first construct the list of elements and then find the Query 3 value of the difference between maximum and minimum elements of the list.

Let’s take an example to understand the problem,

Input:Q = 6

Query (1, 4)

Query (1, 9)

Query (1, 6)

Query (2, 4)

Query (1, 3)

Query (3)

Output: 6

## Explanation

In the end, the list will be {9, 6, 3}.

Maximum -> 9

Minimum -> 3

Difference -> 6

## Solution Approach

A simple approach to solving the problem is using direct solving of each query. By following these steps−

• Initialize an array.

• For query type 1, add an element to the array

• For query type 2, remove an element from the array

• For query type 3, find the difference between maximum and minimum, and return the value.

## Example

Live Demo

#include <iostream>
using namespace std;
void solveQuerry(int type, int item){
int list;
static int index = 0;
if(type == 1){
list[index] = item;
index++;
}
else if(type == 2){
for(int i = 0; i <= index; i++)
if(list[i] == item ){
list[i] = 0;
break;
}
}
else if(type == 3){
int max = -100, min = 100;
for(int i = 0; i< index; i++){
if(list[i] == 0)
i++;
if(list[i] > max)
max = list[i];
if(list[i] < min)
min = list[i];
}
cout<<"The difference between the maximum and minimum elements of the list is "<<(max - min);
}
}
int main() {
int Q = 6;
int query[Q] = {{1, 5}, {1, 9}, {1, 6}, {2, 4}, {1, 3}, {3, 0}};
for(int i = 0; i< Q; i++){
solveQuerry(query[i], query[i]);
}
}

## Output

The difference between the maximum and minimum elements of the list is 6

The search process can be more effective if we use other data structures then a simple array. Here, if we use the self-balancing binary search tree instead of the array. The max element will be at the end (accessed using rbegin() method). And the min element will be at the start (accessed using begin() method).

The implementation of the self-balancing binary search tree in C++ is done using the set.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
set<int> myList;
void solveQuerry(int type, int num){
if(type == 1){
myList.insert(num);
}
else if(type == 2){
myList.erase(num);
}
else if(type == 3){
int max = *(myList.rbegin());
int min = *(myList.begin());
cout<<"The difference between the maximum and minimum elements of the list is "<<(max - min);
}
}
int main() {
int Q = 6;
int query[Q] = {{1, 5}, {1, 9}, {1, 6}, {2, 4}, {1, 3}, {3, 0}};
for(int i = 0; i< Q; i++){
solveQuerry(query[i], query[i]);
}
}

## Output

The difference between the maximum and minimum elements of the list is 6