C++ Program to Find the Minimum and Maximum Element of an Array Last Updated : 17 Jan, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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 = min(res, arr[i]); return res; } int getMax(int arr[], int n) { int res = arr[0]; for (int i = 1; i < n; i++) res = max(res, arr[i]); return res; } int main() { int arr[] = { 12, 1234, 45, 67, 1 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Minimum element of array: " << getMin(arr, n) << " "; cout << "Maximum element of array: " << getMax(arr, n); return 0; } Output: Minimum element of array: 1 Maximum element of array: 1234 Time Complexity: O(n) Auxiliary Space: O(1), as no extra space is usedRecursive Solution 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) { // If there is single element, return it. // Else return minimum of first element and // minimum of remaining array. return (n == 1) ? arr[0] : min(arr[0], getMin(arr + 1, n - 1)); } int getMax(int arr[], int n) { // If there is single element, return it. // Else return maximum of first element and // maximum of remaining array. return (n == 1) ? arr[0] : max(arr[0], getMax(arr + 1, n - 1)); } int main() { int arr[] = { 12, 1234, 45, 67, 1 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Minimum element of array: " << getMin(arr, n) << " "; cout << "Maximum element of array: " << getMax(arr, n); return 0; } Output: Min of array: 1 Max of array: 1234 Time Complexity: O(n) Auxiliary Space: O(n), as implicit stack is used due to recursion Using Library functions: We can use min_element() and max_element() to find minimum and maximum of array. 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) { return *min_element(arr, arr + n); } int getMax(int arr[], int n) { return *max_element(arr, arr + n); } int main() { int arr[] = { 12, 1234, 45, 67, 1 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Minimum element of array: " << getMin(arr, n) << " "; cout << "Maximum element of array: " << getMax(arr, n); return 0; } Output: Minimum element of array: 1 Maximum element of array: 1234 Time Complexity: O(n) Auxiliary Space: O(1), as no extra space is used Comment More infoAdvertise with us Next Article C++ Program to Find the Minimum and Maximum Element of an Array kartik Follow Improve Article Tags : Searching Recursion C++ Programs C++ DSA Arrays C++ Array Programs +3 More Practice Tags : CPPArraysRecursionSearching Similar Reads 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 C++ Program to Find the Second Largest Element in an Array In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the second largest element in an array in C++. Examples: Input: arr[] = {34, 5, 16, 14, 56, 7, 56} Output: 34 Explanation: The 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 Find Maximum and Minimum Element in a Set in C++ STL In C++, set stores the unique elements in sorted order so it is pretty straightforward to find the minimum and maximum values. In this article, we will learn different methods to find the minimum and maximum values in a set in C++.As the set is sorted, the most efficient way to find the minimum and 3 min read C++ Program to Find Largest Element in an Array In this article, we will learn to write a C++ program to find the largest element in the given array arr of size N. The element that is greater than all other elements is the largest element in the array. Recommended PracticeHelp a Thief!!!Try It! One of the most simplest and basic approaches to fin 2 min read Like