Program to find the minimum (or maximum) element of an array
Given an array, write functions to find minimum and maximum elements in it.
C++
// CPP program to find minimum (or maximum) element // in an array. #include <iostream> 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; } |
chevron_right
filter_none
Java
// 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 anuj_67. |
chevron_right
filter_none
Python3
# Python3 program to find minimum # (or maximum) element in an array # Minimum Function def getMin(arr, n): res = arr[0] for i in range(1,n): res = min(res, arr[i]) return res # Maximum Function def getMax(arr, n): res = arr[0] for i in range(1,n): res = max(res, arr[i]) return res # Driver Program 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 Shreyanshi Arun. |
chevron_right
filter_none
C#
// 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. |
chevron_right
filter_none
PHP
<?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 ?> |
chevron_right
filter_none
Output:
Minimum element of array: 1 Maximum element of array: 1234
Time Complexity:O(n)
Recursive Solution
C++
// CPP program to find minimum (or maximum) element // in an array. #include <iostream> 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; } |
chevron_right
filter_none
Java
// 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 code public 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 */ |
chevron_right
filter_none
Python3
# Python3 program to find minimum # (or maximum) element in an array. def getMin(arr, n): # If there is single element, return it. # Else return minimum of first element # and minimum of remaining array. return min(arr); def getMax( arr, n): # If there is single element, return it. # Else return maximum of first element # and maximum of remaining array. return max(arr); # Driver code 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 # Mohit Kumar 29 |
chevron_right
filter_none
C#
// 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 code public 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 contribute by Mohit |
chevron_right
filter_none
Output:
Min of array: 1 Max of array: 1234
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; } |
chevron_right
filter_none
Java
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*/ |
chevron_right
filter_none
Python3
# 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 Code if __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 |
chevron_right
filter_none
C#
// 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 code public 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 |
chevron_right
filter_none
PHP
<?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 ?> |
chevron_right
filter_none
Output:
Minimum element of array: 1 Maximum element of array: 1234
Recommended Posts:
- Sum and Product of minimum and maximum element of an Array
- Leftmost and rightmost indices of the maximum and the minimum element of an array
- Program to find the maximum element in a Matrix
- Find maximum sum taking every Kth element in the array
- Find the minimum element in a sorted and rotated array
- Find the maximum element in an array which is first increasing and then decreasing
- Recursive Programs to find Minimum and Maximum elements of array
- Program to find largest element in an array
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Maximum and minimum of an array using minimum number of comparisons
- Remove exactly one element from the array such that max - min is minimum
- Find maximum element of each row in a matrix
- Find maximum element of each column in a matrix
- Remove all occurrences of any element for maximum array sum
- Maximum element in a very large array using pthreads
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



