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 How to Find the Maximum Element of an Array using STL in C++? Given an array of n elements, the task is to find the maximum element using STL in C++.ExamplesInput: arr[] = {11, 13, 21, 45, 8}Output: 45Explanation: 45 is the largest element of the array.Input: arr[] = {1, 9, 2, 5, 7}Output: 9Explanation: 9 is the largest element of the array.STL provides the fo 3 min read C++ Program to Find k maximum elements of array in original order Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note : k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their app 3 min read C++ Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix 3 min read How to Find the Second Smallest Element in an Array in C++? In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element 3 min read How to Find the Range of Numbers in an Array in C++? The range of numbers in an array means the difference between the maximum value and the minimum value in the given array. In this article, we will learn how to find the range of numbers in an array in C++. For Example, Input: myArray = {5,10,15,30,25}; Output: Range: 25Range Within an Array in C++To 2 min read Like