How to Find the 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 maximum element using STL in C++.
Examples
Input: arr[] = {11, 13, 21, 45, 8}
Output: 45
Explanation: 45 is the largest element of the array.
Input: arr[] = {1, 9, 2, 5, 7}
Output: 9
Explanation: 9 is the largest element of the array.
STL provides the following different methods to find the maximum element in the array in C++:
Using std::max_element()
STL provides the std::max_element() function, which can be used to find the maximum element in the given array.
Syntax
std::max_element(arr, arr + n);
where, arr is the array and n is the number of elements in the array.
Example
C++
// C++ Program to find the maximum element
// in an array using std::max_element()
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {11, 13, 21, 45, 8};
int n = sizeof(arr) / sizeof(arr[0]);
// Find the maximum element
cout << *max_element(arr, arr + n);
return 0;
}
Time Complexity: O(n), where n is the number of elements in the array.
Auxiliary Space: O(1)
Using std::minmax_element()
STL also have the std::minmax_element() function, which can find the minimum and maximum element both at once in the given range. It returns a pair in which pair::first member is minimum, and pair::second member is maximum.
Syntax
std::minmax(arr, arr + n);
where, arr is the array and n is the number of elements in the array.
Example
C++
// C++ Program to find the maximum element
// in an array using std::minmax_element()
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {11, 13, 21, 45, 8};
int n = sizeof(arr) / sizeof(arr[0]);
// Find the maximum element
cout << minmax_element(arr, arr + n)->second;
return 0;
}
Time Complexity: O(n), where n is the number of elements in the array.
Auxiliary Space: O(1)
Using Priority Queue
As we know, C++ priority queue is by default implemented as max heap where the largest element will be at the top. We can create a priority queue from all the array elements and the largest element will be present at the top of the queue.
Example
C++
// C++ Program to find the maximum element
// in an array using priority queue
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {11, 13, 21, 45, 8};
int n = sizeof(arr) / sizeof(arr[0]);
// Creating priority queue using array arr
priority_queue<int> pq(arr, arr + n);
// The top element will be the largest
// element of an array
cout << pq.top();
return 0;
}
Time Complexity: O(n), where n is the number of elements in the array.
Auxiliary Space: O(n)
Using std::sort()
If we sort an array in ascending order, we will have the maximum element at the end of the array. We can sort array using std::sort() function.
Example
C++
// C++ Program to find the maximum element
// in an array using std::sort()
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {11, 13, 21, 45, 8};
int n = sizeof(arr) / sizeof(arr[0]);
// Sort the array
sort(arr, arr + n);
// Last element will the largest element of
// an array as array is sorted in ascending
// order
cout << arr[n - 1];
return 0;
}
Time Complexity: O(n * log n), where n is the number of elements in the array.
Auxiliary Space: O(log n)
Similar Reads
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 and Maximum Element of an Array Using STL in C++? 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 =
3 min read
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 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 Maximum Element in a List in C++? In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows us to store data in non-contiguous memory locations efficiently. In this article, we will learn how to find the maximum element in a list in C++. Example: Input: myList = {30, 20, 10, 5
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