The Wayback Machine - https://web.archive.org/web/20240828111833/https://www.geeksforgeeks.org/how-to-find-the-maximum-element-of-an-array-using-stl-in-c/
Open In App

How to find the maximum element of an Array using STL in C++?

Last Updated : 08 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an array arr[], find the maximum element of this array using STL in C++.
Example: 
 

Input: {1, 45, 54, 71, 76, 12}
Output: 76

Input: {1, 7, 5, 4, 6, 12}
Output: 12

Approach: Max or Maximum element can be found with the help of *max_element() function provided in STL.
Syntax: 
 

*max_element (first_index, last_index);

CPP




// C++ program to find the max
// of Array using sort() in STL
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Get the array
    int arr[] = { 1, 45, 54, 71, 76, 12 };
 
    // Compute the sizes
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Print the array
    cout << "Array: ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    // Find the maximum element
    cout << "\nMax Element = " << *max_element(arr, arr + n);
    return 0;
}


Output

Array: 1 45 54 71 76 12 
Max Element = 76

Time Complexity:- O(n)

Auxiliary Space: O(1)

As constant extra space is used.



Similar Reads

How to find the maximum element of a Vector using STL in C++?
Given a vector, find the maximum element of this vector using STL in C++. Example: Input: {1, 45, 54, 71, 76, 12} Output: 76 Input: {1, 7, 5, 4, 6, 12} Output: 12 Approach: Max or Maximum element can be found with the help of *max_element() function provided in STL. Syntax: *max_element (first_index, last_index); C/C++ Code // C++ program to find t
1 min read
Sorting an array according to another array using pair in STL
We are given two arrays. We need to sort one array according to another. Examples: Input : 2 1 5 4 9 3 6 7 10 8 A B C D E F G H I JOutput : 1 2 3 4 5 6 7 8 9 10 B A F D C G H J E I Here we are sorting second array(a character array) according to the first array (an integer array).We have discussed different ways in below post. Sort an array accordi
6 min read
K’th Smallest/Largest Element using STL
Given an array and a number k where k is smaller than size of array, we need to find the k’th smallest element in the given array. Examples: Input : arr[] = {7, 10, 4, 3, 20, 15} k = 2 Output : 4 Smallest element is 3. Second smallest is 4. Input : arr[] = {7, 10, 4, 3, 3, 15} k = 2 Output : 4 Even if there are more than one occurrences of 3, answe
4 min read
How to find common elements between two Arrays using STL in C++?
Given two arrays, find common elements between these two arrays using STL in C++. Example: Input: arr1[] = {1, 45, 54, 71, 76, 12}, arr2[] = {1, 7, 5, 4, 6, 12} Output: {1, 12} Input: arr1[] = {1, 7, 5, 4, 6, 12}, arr2[] = {10, 12, 11} Output: {1, 4, 12} Approach: Common elements can be found with the help of set_intersection() function provided in
2 min read
How to find common elements between two Vector using STL in C++?
Given two vectors, find common elements between these two vectors using STL in C++. Example: Input: vec1 = {1, 45, 54, 71, 76, 12}, vec2 = {1, 7, 5, 4, 6, 12} Output: {1, 12} Input: vec1 = {1, 7, 5, 4, 6, 12}, vec2 = {10, 12, 11} Output: {1, 4, 12} Approach: Common elements can be found with the help of set_intersection() function provided in STL.
2 min read
How to find the sum of elements of a Vector using STL in C++?
Given a vector, find the sum of the elements of this vector using STL in C++. Example:Input: vec = {1, 45, 54, 71, 76, 12} Output: 259 Input: vec = {1, 7, 5, 4, 6, 12} Output: 35Approach: Sum can be found with the help of accumulate() function provided in STL. Syntax:accumulate(first_index, last_index, initial value of sum); C/C++ Code // C++ progr
2 min read
Find all unique subsets of a given set using C++ STL
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets, using C++ STL. Note: Each subset should be sorted. Examples: Input: N = 3, arr[] = {2, 1, 2}Output:(), (1), (1 2), (1 2 2), (2), (2 2)Explanation: All possible subsets = (), (2), (1), (1, 2), (2), (2 2), (2 1), (2, 1, 2)After S
3 min read
Find and print duplicate words in std::vector&lt;string&gt; using STL functions
Consider an array of string and find duplicate words in that array and print that duplicate words if exist.Examples: Input : { "welcome", "to", "geeks", "for", "geeks" } Output : geeks Input : { "reduce", "reuse", "recycle", "reduce", "reuse", "recycle", " recycle" } Output : recycle reduce reuse Input : { "Go", "Green" } Output : No Duplicate word
2 min read
array::front() and array::back() in C++ STL
Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::front() This function is used to reference the first element of the array container. This function can be used to fetch the first element of an array. Synt
3 min read
array::fill() and array::swap() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::fill() This function is used to set a common value for all the elements of the array container. Syntax : arrayname.fill(value) Parameters : The value to be
3 min read
array::begin() and array::end() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::begin() begin() function is used to return an iterator pointing to the first element of the array container. begin() function returns a bidirectional iterat
3 min read
array::cbegin() and array::cend() in C++ STL
array::cbegin() is a built-in function in C++ STL which returns a const_iterator pointing to the first element in the array. It cannot be used to modify the element in the array which is possible using array::begin(). Syntax: array_name.cbegin() Parameters: The function does not accept any parameters. Return Value: The function returns a const_iter
2 min read
array::rbegin() and array::rend() in C++ STL
array::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container. Syntax:array_name.rbegin()Parameters: The function does not accept any parameter. Return value: The function returns a reverse iterator pointing to the last element in the container. Program to demonstrate the array::rbe
2 min read
How to delete last element from a List in C++ STL
Given a List, the task is to remove the last element from this List in C++. Examples: Input: list = [10 20 30 70 80 90 100 40 50 60] Output: 10 20 30 40 50 60 70 80 90 Input: list = [1 2 3 4 5] Output: 1 2 3 4 Lists are a type of associative containers in which each element has to be unique because the value of the element identifies it. The value
3 min read
Get Map Element at Offset in C++ STL
Prerequisites: Map in C++ STL Since the map is not indexed as arrays or vectors sequential access is not possible in the map but to access an element at a particular offset. Example: inserted elements are { 'a',11 } , { 'b',12 } , { ' c ', 13 } then we can get { 'b', 12 } for 2 position. Methods to Get Map Element at Offset in C++ STL Using an iter
3 min read
Different Ways to Remove an Element From Set in C++ STL
Prerequisite: Set in C++ There are multiple ways to remove an element from the set. These are as follows: Removing an element by its valueRemoving an element by its indexRemoving an element by an iterator Example: Input set s={10, 20, 30, 40, 50} , value=40 // Removing 40 from the set Output set s={10, 20, 30, 50} 1. Removing an element by its valu
3 min read
How to sort an Array using STL in C++?
Given an array arr[], sort this array using STL in C++. Example: Input: arr[] = {1, 45, 54, 71, 76, 12} Output: {1, 12, 45, 54, 71, 76} Input: arr[] = {1, 7, 5, 4, 6, 12} Output: {1, 4, 5, 6, 7, 12} Approach: Sorting can be done with the help of sort() function provided in STL. Syntax: sort(arr, arr + n); // C++ program to sort Array // using sort(
1 min read
All permutations of an array using STL in C++
Given an array, the task is to print or display all the permutations of this array using STL in C++. Examples: Input: a[] = {1, 2, 3} Output: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 Input: a[] = {10, 20, 30, 40} Output: 10 20 30 40 10 20 40 30 10 30 20 40 10 30 40 20 10 40 20 30 10 40 30 20 20 10 30 40 20 10 40 30 20 30 10 40 20 30 40 10 20 40 10 30 20
3 min read
Elements present in first array and not in second using STL in C++
Given two arrays, the task is that we find numbers which are present in first array, but not present in the second array, using STL in C++ Examples: Input: a[] = {1, 2, 3, 4, 5, 10}, b[] = {2, 3, 1, 0, 5} Output: 4 10 Input:a[] = {4, 3, 5, 9, 11}, b[] = {4, 9, 3, 11, 10}; Output: 5 Approach: In STL, the set_difference() method can be used to find t
2 min read
Remove duplicate elements in an Array using STL in C++
Given an array, the task is to remove the duplicate elements from the array using STL in C++ Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} Approach: This can be done using set in standard template library. Set type variable in STL automatically removes duplica
2 min read
Count number of unique Triangles using STL | Set 1 (Using set)
We are given n triangles along with length of their three sides as a,b,c. Now we need to count number of unique triangles out of these n given triangles. Two triangles are different from one another if they have at least one of the sides different.Example: Input: arr[] = {{1, 2, 2}, {4, 5, 6}, {4, 5, 6} Output: 2 Input: arr[] = {{4, 5, 6}, {6, 5, 4
3 min read
multimap find() in C++ STL
multimap::find() is a built-in function in C++ STL which returns an iterator or a constant iterator that refers to the position where the key is present in the multimap. In case of multiple same keys being present, the iterator that refers to one of the keys (typically the first one). In case we wish to get all the items with a given key, we may us
2 min read
set find() function in C++ STL
The set::find is a built-in function in C++ STL which returns an iterator to the element which is searched in the set container. If the element is not found, then the iterator points to the position just after the last element in the set. Syntax: set_name.find(element) Parameters: The function accepts one mandatory parameter element which specifies
2 min read
unordered_multimap find() function in C++ STL
The unordered_multimap::find() is a built-in function in C++ STL which returns an iterator which points to one of the elements which has the key k. If the container does not contain any element with key k, then it returns an iterator which points to the position which is past the last element in the container. Syntax: unordered_multimap_name.find(k
3 min read
multiset find() function in C++ STL
The multiset::find() is a built-in function in C++ STL which returns an iterator pointing to the lower_bound of the element which is searched in the multiset container. If the element is not found, then the iterator points to the position past the last element in the set. Syntax: multiset_name.find(element) Parameters: The function accepts one mand
2 min read
unordered_multiset find() function in C++STL
The unordered_multiset::find() is a built-in function in C++ STL which returns an iterator which points to the position which has the element val. If the element does not contain the element val, then it returns an iterator which points to a position past the last element in the container. Syntax: unordered_multiset_name.find(val) Parameters: The f
2 min read
map find() function in C++ STL
The map::find() is a built-in function in C++ STL that returns an iterator or a constant iterator that refers to the position where the key is present in the map. If the key is not present in the map container, it returns an iterator or a constant iterator which refers to map.end() . Syntax: iterator=map_name.find(key) or constant iterator=map_name
3 min read
unordered_set find() function in C++ STL
The unordered_set::find() function is a built-in function in C++ STL which is used to search for an element in the container. It returns an iterator to the element, if found else, it returns an iterator pointing to unordered_set::end(). Syntax : unordered_set_name.find(key)Parameter: This function accepts a mandatory parameter key which specifies t
2 min read
unordered_map find in C++ STL
In C++, std::unordered_map::find function is used to search for a specific key in an unordered map. This function is a member function of std::unordered_map container Syntaxunordered_map.find(key);ParametersIt takes only one parameter. key: The key that is to be searched.Return ValueIf the given key exists in unordered_map, it returns an iterator t
2 min read
Maximum element in a sorted and rotated array
Given a sorted array arr[] of distinct elements which is rotated at some unknown point, the task is to find the maximum element in it.Examples: Input: arr[] = {3, 4, 5, 1, 2} Output: 5Input: arr[] = {1, 2, 3} Output: 3 Approach: A simple solution is to traverse the complete array and find maximum. This solution requires O(n) time. We can do it in O
6 min read
Article Tags :
Practice Tags :