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

15 Jun, 2022
Given an array, write functions to find the minimum and maximum elements in it. 
C++
C
Java
Python3
C#
PHP
Javascript
// 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 program to find minimum (or maximum) element// in an array.#include <stdio.h> // Find maximum between two numbers.int max(int num1, int num2){    return (num1 > num2) ? num1 : num2;} // Find minimum between two numbers.int min(int num1, int num2){    return (num1 > num2) ? num2 : num1;} 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]);    printf("Minimum element of array: %d \n",           getMin(arr, n));    printf("Maximum element of array: %d \n",           getMax(arr, n));    return 0;} // This code is contributed by Aditya Kumar (adityakumar129)
// Java program to find minimum (or maximum)// element in an array.import java.io.*; class GFG {     static int getMin(int arr[], int n)    {        int res = arr[0];        for (int i = 1; i < n; i++)            res = Math.min(res, arr[i]);        return res;    }     static int getMax(int arr[], int n)    {        int res = arr[0];        for (int i = 1; i < n; i++)            res = Math.max(res, arr[i]);        return res;    }     // Driver code    public static void main(String[] args)    {        int arr[] = { 12, 1234, 45, 67, 1 };        int n = arr.length;        System.out.println("Minimum element of array: " + getMin(arr, n));        System.out.println("Maximum element of array: " + getMax(arr, n));    }} // This code is contributed by Aditya Kumar (adityakumar129)
# Python3 program to find minimum# (or maximum) element in an array # Minimum Functiondef getMin(arr, n):    res = arr[0]    for i in range(1,n):        res = min(res, arr[i])    return res # Maximum Functiondef getMax(arr, n):    res = arr[0]    for i in range(1,n):        res = max(res, arr[i])    return res # Driver Programarr = [12, 1234, 45, 67, 1]n = len(arr)print ("Minimum element of array:", getMin(arr, n))print ("Maximum element of array:", getMax(arr, n)) # This code is contributed# by Shreyanshi Arun.
// C# program to find// minimum (or maximum)// element in an array.using System; class GFG{    static int getMin(int []arr,                      int n)    {        int res = arr[0];                 for (int i = 1; i < n; i++)            res = Math.Min(res, arr[i]);        return res;    }         static int getMax(int []arr,                      int n)    {        int res = arr[0];                 for (int i = 1; i < n; i++)            res = Math.Max(res, arr[i]);        return res;    }     // Driver code    public static void Main ()    {        int []arr = {12, 1234, 45, 67, 1};        int n = arr.Length;        Console.Write("Minimum element" +                          " of array: " +                         getMin(arr, n) + "\n" );        Console.Write("Maximum element" +                          " of array: " +                         getMax(arr, n));    }} // This code is contributed by Smita.
<?php// PHP program to find minimum// (or maximum) element in an// array. function getMin($arr, $n){    $res = $arr[0];    for ($i = 1; $i < $n; $i++)        $res = min($res, $arr[$i]);    return $res;} function getMax($arr, $n){    $res = $arr[0];    for ($i = 1; $i < $n; $i++)        $res = max($res, $arr[$i]);    return $res;}     // Driver Code    $arr = array(12, 1234, 45, 67, 1);    $n = sizeof($arr);    echo "Minimum element of array: "           , getMin($arr, $n), "\n";    echo "Maximum element of array: "                  ,getMax($arr, $n); // This code is contributed by ajit?>
<script> // JavaScript program to find minimum// (or maximum) element in an array.function getMin(arr, n){    let res = arr[0];          for(let i = 1; i < n; i++)        res = Math.min(res, arr[i]);             return res;}  function getMax(arr, n){    let res = arr[0];          for(let i = 1; i < n; i++)        res = Math.max(res, arr[i]);             return res;} // Driver Codelet arr = [ 12, 1234, 45, 67, 1 ];let n = arr.length; document.write("Minimum element" +               " of array: " +               getMin(arr, n) + "<br/>");document.write("Maximum element" +               " of array: " +               getMax(arr, n));                // This code is contributed by sanjoy_62 </script>
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 
C++
Java
Python3
C#
Javascript
// 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;}
// Java program to find minimum// (or maximum) element// in an array.class GFG{ static int getMin(int arr[], int i, int n){    // If there is single element, return it.    // Else return minimum of first element and    // minimum of remaining array.    return (n == 1) ? arr[i] : Math.min(arr[i],                        getMin(arr,i + 1 , n - 1));} static int getMax(int arr[], int i, int n){    // If there is single element, return it.    // Else return maximum of first element and    // maximum of remaining array.    return (n == 1) ? arr[i] : Math.max(arr[i],                         getMax(arr ,i + 1, n - 1));} // Driver codepublic static void main(String[] args){    int arr[] = { 12, 1234, 45, 67, 1 };    int n = arr.length;    System.out.print("Minimum element of array: " +                        getMin(arr, 0, n) + "\n");    System.out.println("Maximum element of array: " +                        getMax(arr, 0, n));    }} /* This code contributed by PrinciRaj1992 */
# Python3 program to find minimum# (or maximum) element in an array.def getMin(arr, n):    if(n==1):        return arr[0]    # If there is single element, return it.    # Else return minimum of first element    # and minimum of remaining array.    else:        return min(getMin(arr[1:], n-1), arr[0])def getMax(arr, n):    if(n==1):        return arr[0]    # If there is single element, return it.    # Else return maximum of first element    # and maximum of remaining array.    else:        return max(getMax(arr[1:], n-1), arr[0]) # Driver codearr = [12, 1234, 45, 67, 1]n = len(arr)print("Minimum element of array: ",                   getMin(arr, n));print("Maximum element of array: ",                   getMax(arr, n)); # This code is contributed by# Mohit Kumar 29
// C# program to find minimum// (or maximum) element// in an array.using System; class GFG{ static int getMin(int []arr, int i, int n){    // If there is single element, return it.    // Else return minimum of first element and    // minimum of remaining array.    return (n == 1) ? arr[i] : Math.Min(arr[i],                    getMin(arr,i + 1 , n - 1));} static int getMax(int []arr, int i, int n){    // If there is single element, return it.    // Else return maximum of first element and    // maximum of remaining array.    return (n == 1) ? arr[i] : Math.Max(arr[i],                    getMax(arr ,i + 1, n - 1));} // Driver codepublic static void Main(String[] args){    int []arr = { 12, 1234, 45, 67, 1 };    int n = arr.Length;    Console.WriteLine("Minimum element of array: " +                                getMin(arr, 0, n));    Console.WriteLine("Maximum element of array: " +                                getMax(arr, 0, n));}} // This code is contributed by Mohit
<script>// javascript program to find minimum// (or maximum) element// in an array.   function getMin(arr , i , n) {        // If there is single element, return it.        // Else return minimum of first element and        // minimum of remaining array.        return (n == 1) ? arr[i] : Math.min(arr[i], getMin(arr, i + 1, n - 1));    }     function getMax(arr , i , n) {        // If there is single element, return it.        // Else return maximum of first element and        // maximum of remaining array.        return (n == 1) ? arr[i] : Math.max(arr[i], getMax(arr, i + 1, n - 1));    }     // Driver code             var arr = [ 12, 1234, 45, 67, 1 ];        var n = arr.length;        document.write("Minimum element of array: " + getMin(arr, 0, n) + "<br/>");        document.write("Maximum element of array: " + getMax(arr, 0, n)); // This code contributed by Rajput-Ji</script>
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. 
C++
Java
Python3
C#
PHP
Javascript
// 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;}
import java.util.Arrays; // Java program to find minimum (or maximum) element// in an array.import java.util.Arrays; class GFG {     static int getMin(int arr[], int n) {        return Arrays.stream(arr).min().getAsInt();    }     static int getMax(int arr[], int n) {        return Arrays.stream(arr).max().getAsInt();    } // Driver code    public static void main(String[] args) {        int arr[] = {12, 1234, 45, 67, 1};        int n = arr.length;        System.out.println("Minimum element of array: " + getMin(arr, n));        System.out.println("Maximum element of array: " + getMax(arr, n));    }}/*This Java code is contributed by 29AjayKumar*/
# Python3 program to find minimum# (or maximum) element# in an array.def getMin(arr,n):    return min(arr) def getMax(arr,n):    return max(arr) # Driver Codeif __name__=='__main__':    arr = [12,1234,45,67,1]    n = len(arr)    print("Minimum element of array: "          ,getMin(arr, n))    print("Maximum element of array: "          ,getMax(arr, n)) # This code is contributed by# Shrikant13
// C# program to find minimum// (or maximum) element in an array.using System;using System.Linq;class GFG{static int getMin(int []arr, int n){    return arr.Min();} static int getMax(int []arr, int n){    return arr.Max();} // Driver codepublic static void Main(String[] args){    int []arr = {12, 1234, 45, 67, 1};    int n = arr.Length;    Console.WriteLine("Minimum element of array: " +                                    getMin(arr, n));    Console.WriteLine("Maximum element of array: " +                                    getMax(arr, n));}} // This code is contributed by 29AjayKumar
<?php// PHP program to find minimum (or maximum)// element in an array. function getMin(&$arr, $n){    return min($arr);} function getMax(&$arr, $n){    return max($arr);} // Driver Code$arr = array(12, 1234, 45, 67, 1 );$n = sizeof($arr);echo "Minimum element of array: " .           getMin($arr, $n) . "\n";echo "Maximum element of array: " .                  getMax($arr, $n); // This code is contributed// by ChitraNayal?>
<script> // Javascript program to find// minimum (or maximum) element// in an array.     function getMin(arr , n)    {        return Math.min.apply(Math,arr);    }     function getMax(arr , n) {        return Math.max.apply(Math,arr);    }     // Driver code             var arr = [ 12, 1234, 45, 67, 1 ];        var n = arr.length;        document.write("Minimum element of array: " +        getMin(arr, n)+"<br/>");        document.write("Maximum element of array: "        + getMax(arr, n)); // This code contributed by aashish1995 </script>
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
This article is contributed by Aarti_Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
vt_m
Smitha Dinesh Semwal
shrikanth13
29AjayKumar
ukasp
mohit kumar 29
princiraj1992
shantanutheone
sanjoy_62
aashish1995
Rajput-Ji
adityakumar129
simmytarika5
_shinchancode
CBSE - Class 11
school-programming
Recursion
School Programming
Searching
Searching
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Backtracking | Introduction
Print all subsequences of a string
Reverse a stack using recursion
Check if a number is Palindrome
Count all possible paths from top left to bottom right of a mXn matrix
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java