Thursday, September 18, 2025
HomeData Modelling & AIC++ Program to Find the Minimum and Maximum Element of an Array

C++ Program to Find the Minimum and Maximum Element of an Array

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 used

Recursive 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

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6664 POSTS0 COMMENTS
Nicole Veronica
11835 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7053 POSTS0 COMMENTS
Thapelo Manthata
6736 POSTS0 COMMENTS
Umr Jansen
6743 POSTS0 COMMENTS