How to Find Minimum and Maximum Element of an Array Using STL in C++? Last Updated : 28 Oct, 2024 Comments Improve Suggest changes Like Article Like Report Given an array of n elements, the task is to find the minimum and maximum element of array using STL in C++.ExamplesInput: arr[] = {1, 45, 54, 7, 76}Output: min = 1, max = 76Explanation: 1 is the smallest and 76 is the largest among all elements.Input: arr[] = {10, 7, 5, 4, 6}Output: min = 4, max = 10Explanation: 4 is the smallest and 10 is the largest among all elements.STL provides two different ways to find the minimum and maximum element of array in C++:Table of ContentUsing min_element() and max_element()Using minmax_element()Using min_element() and max_element()C++ STL provides two function std::min_element() and std::max_element() to find the minimum element and the maximum element in the given range. We can use these functions to find the smallest and largest element of the array.Syntaxstd::min_element(arr, arr + n);std::max_element(arr, arr + n);where, arr is the name of the array and n is the number of elements in it.Example C++ // C++ program to find the min and max // element of an array using min_element() // and max_element() #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 45, 54, 71, 76}; int n = sizeof(arr) / sizeof(arr[0]); // Find the min element int min = *min_element(arr, arr + n); // Find the max element int max = *max_element(arr, arr + n); cout << "Min: " << min << endl; cout << "Max: " << max; return 0; } OutputMin: 1 Max: 76 Time Complexity: O(n), where n is the number of elements in array.Auxiliary Space: O(1)Using minmax_element()C++ STL also provides the std::minmax_element() function that is used to find both the minimum and the maximum element in the range in a single function call. This function returns a reference of std::pair object in which pair::first is the minimum element and pair::second is the maximum element.Syntaxstd::minmax_element(arr, arr + n);where, arr is the name of the array and n is the number of elements in it.Example C++ // C++ program to find the min and max element // of an array using minmax_element() #include <bits/stdc++.h> using namespace std; int main() { int arr[] = { 1, 45, 54, 71, 76}; int n = sizeof(arr)/sizeof(arr[0]); // Finding minimum and maximum element auto minmax = minmax_element(arr, arr + n); cout << "Min: " << *minmax.first << endl; cout << "Max: " << *minmax.second; return 0; } OutputMin: 1 Max: 76 Time Complexity: O(n), where n is the number of elements in array.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find Minimum and Maximum Element of an Array Using STL in C++? C code_r Follow Improve Article Tags : Misc C++ Programs C++ STL cpp-array CPP Examples +2 More Practice Tags : CPPMiscSTL Similar Reads How to Find the Minimum and Maximum Element of a Vector Using STL in C++? In this article, we will learn how to find the minimum and maximum element in vector in C++.The simplest method to find the minimum and maximum element in vector is by using min_element() and max_element(). Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; 2 min read How to Find the Maximum Element of an Array using STL in C++? Given an array of n elements, the task is to find the maximum element using STL in C++.ExamplesInput: arr[] = {11, 13, 21, 45, 8}Output: 45Explanation: 45 is the largest element of the array.Input: arr[] = {1, 9, 2, 5, 7}Output: 9Explanation: 9 is the largest element of the array.STL provides the fo 3 min read C++ Program to Find the Minimum and Maximum Element of an Array Given an array, write functions to find the minimum and maximum elements in it. Example: C++ // C++ program to find minimum (or maximum) element // in an array. #include <bits/stdc++.h> using namespace std; int getMin(int arr[], int n) { int res = arr[0]; for (int i = 1; i < n; i++) res = 3 min read Find Maximum and Minimum Element in a Set in C++ STL In C++, set stores the unique elements in sorted order so it is pretty straightforward to find the minimum and maximum values. In this article, we will learn different methods to find the minimum and maximum values in a set in C++.As the set is sorted, the most efficient way to find the minimum and 3 min read How to Find the Maximum Element of a Vector using STL in C++? Given a vector, find the maximum element of the vector using STL in C++. ExampleInput: v = {2, 4, 1, 5, 3}Output: 5Explanation: 5 is the largest element of vector.Input: v = {11, 23, 3, 5, 24}Output: 24Explanation: 24 is the largest element of the given range.STL provides the following different met 3 min read How to Find Minimum Element in a Vector in C++? Given a vector of n elements, the task is to find the minimum element using C++.ExamplesInput: v = {2, 4, 1, 5, 3}Output: 1Explanation: 1 is the smallest element in the vectorInput: v = {12, 34, 5, 7}Output: 5Explanation: 5 is the smallest element in the vector.Following are the 6 different methods 4 min read How to Find the Minimum Element in a List in C++? In C++, a list is a sequence container provided by the STL library that stores data in non-contiguous memory locations efficiently. In this article, we will learn how to find the minimum element in a list in C++. Example: Input: myList = {30, 10, 20, 50, 40};Output: The minimum element in the list i 2 min read How to Sort an Array in Descending Order using STL in C++? Sort an array in descending order means arranging the elements in such a way that the largest element at first place, second largest at second place and so on. In this article, we will learn how to sort an array in descending order using STL in C++. ExamplesInput: arr[] = {11, 9, 45, 21};Output: 78 4 min read How to Find the Second Smallest Element in an Array in C++? In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element 3 min read 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: 3Finding the Minim 2 min read Like