How to Find the Maximum Element of an Array using STL in C++?
Last Updated :
04 Nov, 2024
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
How to Find the Maximum Element in a Deque in C++? in C++, double-ended queues, also known as deques, are sequence containers with the feature of insertion and deletion on both ends. They are similar to vectors but are more efficient for the insertion and deletion of elements from both ends. In this article, we will learn how to find the maximum ele
2 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
How to Find the Mode of Numbers in an Array in C++? Mode of any dataset is the item that occurs most frequently in it. In this article, we will find the mode of numbers in an unsorted array in C++. For Example,Input: myArray = { 1, 2, 3, 4, 5, 2, 3, 2, 2, 4, 2 } Output: Mode : 2Finding Mode of Array Elements in C++To find the mode of the array elemen
2 min read
How to Find the Range of Numbers in an Array in C++? The range of numbers in an array means the difference between the maximum value and the minimum value in the given array. In this article, we will learn how to find the range of numbers in an array in C++. For Example, Input: myArray = {5,10,15,30,25}; Output: Range: 25Range Within an Array in C++To
2 min read