Design a queue data structure to get minimum or maximum in O(1) time


C++ has a deque header file that handles the properties of stack and queue. In data structure for solving the problem in O(1) time complexity, it requires constant time. By using deque to this program we get the advantage to use both stack and queue.

In this article, we are going to solve the queue data structure to get the minimum or maximum of a number in O(1) time.

Syntax

deque<data_type> name_of_queue;

Parameters

  • deque − This is known for the double-ended queue that ordered a group of items or numbers equivalent to the queue.

  • data_type − The type of data used like int, float, etc.

  • name_of_queue − Any name given to the queue like ab, cd, etc.

front()

The front() is a predefined function in C++ STL which directly references the first index position of a queue.

back()

The back() is a predefined function in C++ STL which directly references the last index position of a queue.

push_back()

The push_back() is also a predefined function that is used for inserting the element from the back.

Algorithm

  • We will start the program with header files namely ‘iostream’ and ‘deque’.

  • We are inserting the double-ended queue to handle the maximum or minimum of a number.

    • “deque<int> dq” − By using this we are enabling the properties of stack and queue

  • Start with for loop, we are inserting an element from the range of 10 to 15. Then using a method named ‘push_back[i]’ which accepts ‘i’ as a parameter to push the array element using a for loop.

  • Then we are creating two variables to find the minimum and maximum of a number by use of predefined function front() and back(). The front() finds the first index to present the minimum number whereas the back() finds the last index to present the maximum number.

  • Now we are initializing for loop to iterate the index number length and using this length we will categorize the comparison of minimum and maximum elements to ‘dq[i]’. So this will find the minimum and maximum number.

  • Finally, we are printing the output of minimum and maximum length with help of ‘min_element’ and ‘max_element’ variables.

Example

In this program, we are going to solve queue data structure to get minimum and maximum in O(1) time.

#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> dq; 
   // double ended queue
   // insert elements into the deque using a loop
   for(int i = 10; i <= 15; i++) {
      dq.push_back(i);
   }
   // find the minimum and maximum elements
   int min_element = dq.front();
   int max_element = dq.back();

   for(int i = 1; i < dq.size(); i++) {
      if(dq[i] < min_element) {
         min_element = dq[i];
      }
      if(dq[i] > max_element) {
         max_element = dq[i];
      }
   }
   //Print the minimum and maximum elements
   cout << "Minimum element: " << min_element << endl;
   cout << "Maximum element: " << max_element << endl;
   return 0;
}

Output

Minimum element: 10
Maximum element: 15

Conclusion

We explored the concept of queue data structure to find the minimum or maximum element. We saw how front() and back() are useful to find the min and max of elements and also see how pushback is added to the end of the index element. By using deque we handle the problem in O(1) time complexity.

Updated on: 10-May-2023

461 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements