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

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

Here we will see how to find the maximum and minimum element from an array. So if the array is like [12, 45, 74, 32, 66, 96, 21, 32, 27], then max element is 96, and min element is 12. We can use the max_element() function and min_element() function, present in algorithm.h header file to get the maximum and minimum elements respectively.

## Example

Live Demo

#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int arr[] = {12, 45, 74, 32, 66, 96, 21, 32, 27};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Array is like: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << "Max Element is: " << *max_element(arr, arr + n);
cout << "Min Element is: " << *min_element(arr, arr + n);
}

## Output

Array is like: 12 45 74 32 66 96 21 32 27
Max Element is: 96
Min Element is: 12
Updated on 17-Dec-2019 13:09:59