C++ Program to Find the Minimum and Maximum Element of an Array Last Updated : 17 Jan, 2023 Summarize Comments Improve Suggest changes Share 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 std::min_element in C++ K kartik Follow Improve Article Tags : C++ C++ Array Programs Practice Tags : CPP Similar Reads std::minmax() and std::minmax_element() in C++ STL C++ defined functions to get smallest and largest elements among 2 or in a container using different functions. But there are also functions that are used to get both smallest and largest element using a single function, "minmax()" function achieves this task for us. This function is defined in "alg 4 min read Minimum and Maximum of all subarrays of size K using Map Given an array arr[] of N integers and an integer K, the task is to find the minimum and maximum of all subarrays of size K. Examples: Input: arr[] = {2, -2, 3, -9, -5, -8}, K = 4 Output: -9 3 -9 3 -9 3 Explanation: Below are the subarray of size 4 and minimum and maximum value of each subarray: 1. 9 min read std::min_element in C++ The std::min_element() in C++ is an STL algorithm that is used to find the minimum element in a given range. This range can be array, vector, list or any other container. It is defined inside the <algorithm> header file. In this article, we will learn about the std::min_element() in C++.Exampl 4 min read max_element in C++ STL The std::max_element() in C++ is an STL algorithm that is used to find the maximum element in the given range. It is defined inside the <algorithm> header file. In this article, we will learn how to find the maximum element in the range using std::max_element() in C++.Example:C++// C++ program 4 min read C / C++ Program for Largest Sum Contiguous Subarray Write a C/C++ program for a given array arr[] of size N. The task is to find the sum of the contiguous subarray within a arr[] with the largest sum. Recommended: Please solve it on âPRACTICE â first, before moving on to the solution. C / C++ Program for Largest Sum Contiguous Subarray using Kadane's 5 min read std::max in C++ The std::max() is the built-in function in C++ used for finding the maximum among two or more elements passed to it as arguments. It is defined inside <algorithm> header file. In this article, we will learn how to use std::max() function in C++.The std::max() can be used in the following ways: 2 min read Like