Open In App

How to Find the Minimum Element in a Deque in C++?

In C++, a deque (double-ended queue) container is a dynamic array that allows insertions and deletions at both ends. In this article, we will learn how to find the minimum element in a deque in C++.

Example:

Input:
myDeque = {10, 5, 8, 3, 12};

Output:
Minimum Element in the Deque: 3

Finding the Minimum Element in a Deque in C++

To find the minimum element in a std::deque container, we can use the std::min_element() algorithm provided in STL that takes the iterator to the first and last of the range and returns the iterator to the minimum element in the range.

Syntax to Use std::min_element()

min_element (first, last);

Here,

C++ Program to Find the Minimum Element in a Deque

The below example demonstrates how we can find the minimum element in a deque in C++.

// C++ Program to demonstrates how we can find the minimum
// element in a deque
#include <algorithm>
#include <deque>
#include <iostream>

using namespace std;

int main()
{

    // Creating a deque with some elements
    deque<int> myDeque = { 10, 5, 8, 3, 12 };

    // Finding the minimum element using min_element
    // function
    auto res = min_element(myDeque.begin(), myDeque.end());

    // Checking if min_element found a valid minimum element
    if (res != myDeque.end()) {
        cout << "Minimum Element in the Deque: " << *res
             << endl;
    }
    else {
        cout << "Deque is empty." << endl;
    }

    return 0;
}

Output
Minimum Element in the Deque: 3

Time Complexity: O(N), here N is the number of elements in the deque.
Auxiliary Space: O(1)



Article Tags :