Open In App

How to Find the Maximum Element of an Array using STL in C++?

Last Updated : 04 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
}

Output
45

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;
}

Output
45

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;
}

Output
45

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;
}

Output
45

Time Complexity: O(n * log n), where n is the number of elements in the array.
Auxiliary Space: O(log n)



Article Tags :
Practice Tags :

Similar Reads