**Problem:** Design a Data Structure a SpecialQueue which supports following operations enque, deque, getMin() or getMax() where getMin() operation takes O(1) time.

**Example:**

Let the data to be inserted in queue be - 4, 2, 1, 6 Operation Queue Output push(4) 4 - push(2) 4, 2 - push(1) 4, 2, 1 - getMin() 4, 2, 1 1 push(6) 4, 2, 1, 6 - pop() 2, 1, 6 4 pop() 1, 6 2 pop() 6 1 getMin() 6 6 // Notice the getMin() function call // It returns the minimum element // of all the values present in the queue

**Approach:** The idea is to use Doubly ended Queue to store in increasing order if the structure is to return the minimum element and store in decreasing order if the structure is to return the maximum element. The operations of the Data Struture is defined as follows:

**Enque**

- Insert the element into the queue structure.
- If the size of the Deque structure is empty that is the size of the Deque is 0. Then, Insert the element from the back.
- Otherwise, If there are some elements in the Deque structure then pop the elements out from the Deque until the back of the Deque is greater than the current element and then finally insert the element from back.

**Deque**

- If the first element of the Deque is equal to the front element of the queue then pop the elements out from the Queue and the Deque at the same time.
- Otherwise, Pop the element from the front of the queue to maintain the order of the elements.

**Get Minimum**

Return the front element of the Deque to get the minimum element of the current element of the queue.

Below is the implementation of the above approach:

## C++

`// C++ implementation to design ` `// a queue data structure to get ` `// minimum element in O(1) time ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `template` `<` `typename` `T> ` ` ` `// Structure of the queue ` `class` `MinMaxQueue { ` `public` `: ` ` ` ` ` `// Queue to store the ` ` ` `// element to maintain the ` ` ` `// order of insertion ` ` ` `queue<T> Q; ` ` ` ` ` `// Doubly ended queue to ` ` ` `// get the minimum element ` ` ` `// in the O(1) time ` ` ` `deque<T> D; ` ` ` ` ` `// Function to push a element ` ` ` `// into the queue ` ` ` `void` `enque_element(T element) ` ` ` `{ ` ` ` `// If there is no element ` ` ` `// in the queue ` ` ` `if` `(Q.size() == 0) { ` ` ` `Q.push(element); ` ` ` `D.push_back(element); ` ` ` `} ` ` ` `else` `{ ` ` ` `Q.push(element); ` ` ` ` ` `// Pop the elements out ` ` ` `// until the element at ` ` ` `// back is greater than ` ` ` `// current element ` ` ` `while` `(!D.empty() && ` ` ` `D.back() > element) { ` ` ` `D.pop_back(); ` ` ` `} ` ` ` `D.push_back(element); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Function to pop the element ` ` ` `// out from the queue ` ` ` `void` `deque_element() ` ` ` `{ ` ` ` `// Condition when the Minimum ` ` ` `// element is the element at ` ` ` `// the front of the Deque ` ` ` `if` `(Q.front() == D.front()) { ` ` ` `Q.pop(); ` ` ` `D.pop_front(); ` ` ` `} ` ` ` `else` `{ ` ` ` `Q.pop(); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Function to get the ` ` ` `// minimum element from ` ` ` `// the queue ` ` ` `T getMin() ` ` ` `{ ` ` ` `return` `D.front(); ` ` ` `} ` `}; ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `MinMaxQueue<` `int` `> k; ` ` ` `int` `example[3] = { 1, 2, 4 }; ` ` ` ` ` `// Loop to enque element ` ` ` `for` `(` `int` `i = 0; i < 3; i++) { ` ` ` `k.enque_element(example[i]); ` ` ` `} ` ` ` `cout << k.getMin() << ` `"\n"` `; ` ` ` `k.deque_element(); ` ` ` `cout << k.getMin() << ` `"\n"` `; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Static Data Structure vs Dynamic Data Structure
- Applications of Queue Data Structure
- Stack and Queue in Python using queue Module
- Check if a queue can be sorted into another queue using a stack
- Reversing a Queue using another Queue
- Difference between Circular Queue and Priority Queue
- Design a stack to retrieve original elements and return the minimum element in O(1) time and O(1) space
- Difference between Stack and Queue Data Structures
- Structure Member Alignment, Padding and Data Packing
- Commonly Asked Data Structure Interview Questions | Set 1
- Applications of Graph Data Structure
- Inversion Count using Policy Based Data Structure
- Maximum and minimum of an array using minimum number of comparisons
- Minimum difference between maximum and minimum value of Array with given Operations
- Minimum distance between the maximum and minimum element of a given Array
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Real-time application of Data Structures
- Minimum number of points to be removed to get remaining points on one side of axis
- Print the final string when minimum value strings get concatenated in every operation
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.