Open In App

Program to find the minimum (or maximum) element of an array

Last Updated : 03 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
News Follow

Given an array, write functions to find the minimum and maximum elements in it. 

The most simplest way to find min and max value of an element is to use inbuilt function sort() in java. So, that value at 0th position will min and value at nth position will be max.

C++
// C++ code for the approach

#include <iostream>
#include <algorithm>

using namespace std;

// Driver Code
int main() {
      // Input array
    int a[] = { 1, 423, 6, 46, 34, 23, 13, 53, 4 };
    int n = sizeof(a) / sizeof(a[0]);
  
    // Implemented inbuilt function to sort array
    sort(a, a + n);

    // after sorting the value at 0th position will minimum
    // and nth position will be maximum
    cout << "min-" << a[0] << " max-" << a[n - 1] << endl;
  
    return 0;
}
C Java Python C# JavaScript PHP

Output
min-1 max-423

Time complexity : O(n log(n))
Auxiliary Space : O(1)

C++
// CPP 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)
         << "\n";
    cout << "Maximum element of array: " << getMax(arr, n);
    return 0;
}

// This code is contributed by Aditya Kumar (adityakumar129)
C Java Python C# JavaScript PHP

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 

C++
// CPP 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) << "\n";
    cout << "Maximum element of array: " << 
                                   getMax(arr, n);
    return 0;
}
C Java Python C# JavaScript PHP

Output
Minimum element of array: 1
Maximum element 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. 

C++
// CPP 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)
         << "\n";
    cout << "Maximum element of array: " << getMax(arr, n);
    return 0;
}
C Java Python C# JavaScript PHP

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

 



Next Article

Similar Reads

three90RightbarBannerImg