
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Queries to add, remove and return the difference of maximum and minimum in C++
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
#include <iostream> using namespace std; void solveQuerry(int type, int item){ int list[100]; 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][2] = {{1, 5}, {1, 9}, {1, 6}, {2, 4}, {1, 3}, {3, 0}}; for(int i = 0; i< Q; i++){ solveQuerry(query[i][0], query[i][1]); } }
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
#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][2] = {{1, 5}, {1, 9}, {1, 6}, {2, 4}, {1, 3}, {3, 0}}; for(int i = 0; i< Q; i++){ solveQuerry(query[i][0], query[i][1]); } }
Output
The difference between the maximum and minimum elements of the list is 6
- Related Articles
- Return the difference between the maximum & minimum number formed out of the number n in JavaScript
- How to read Maximum and Minimum temperature in Six's Maximum and Minimum Thermometer?
- C++ Program to get difference between maximum and minimum water in barrels
- Queries to return the absolute difference between Lth smallest number and the R-th smallest number in C++
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number in C++ Program
- What Is Maximum and Minimum ?
- Maximum and minimum of an array using minimum number of comparisons in C
- What is the minimum and maximum number of digits in the sum if we add any two 3 digit number
- Minimum and Maximum Priority Threads in Java
- Get maximum and minimum value in MongoDB?
- Maximum and Minimum Product Subsets in C++
- Getting Minimum and Maximum Value in MySQL
- 8085 program to find maximum and minimum of 10 numbers
- Queries for maximum difference between prime numbers in given ranges in C++
- Recursive Programs to find Minimum and Maximum elements of array in C++
