How to Find Minimum and Maximum Element of an Array Using STL in C++?
Last Updated :
11 Jul, 2025
Given an array of n elements, the task is to find the minimum and maximum element of array using STL in C++.
Examples
Input: arr[] = {1, 45, 54, 7, 76}
Output: min = 1, max = 76
Explanation: 1 is the smallest and 76 is the largest among all elements.
Input: arr[] = {10, 7, 5, 4, 6}
Output: min = 4, max = 10
Explanation: 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++:
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.
Syntax
std::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;
}
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.
Syntax
std::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;
}
Time Complexity: O(n), where n is the number of elements in array.
Auxiliary Space: O(1)
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