Open In App

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

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 ``#include ` `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;``}`

## Java

 `import` `java.util.*;``class` `Array {``    ``public` `static` `void` `main(String[] args) {``        ``int` `a[]={``1``,``423``,``6``,``46``,``34``,``23``,``13``,``53``,``4``};``          ` `          ``//Implemented inbuilt function to sort array``        ``Arrays.sort(a);``      ` `          ``// after sorting the value at 0th position will minimum and``        ``//nth position will be maximum``        ``System.out.println(``"min-"``+a[``0``]+``" max-"``+a[a.length-``1``]);``    ``}``}`

## Python3

 `import` `sys` `# Define an array``a ``=` `[``1``, ``423``, ``6``, ``46``, ``34``, ``23``, ``13``, ``53``, ``4``]` `# Sort the array using the built-in sorted() function``a_sorted ``=` `sorted``(a)` `# Find the minimum and maximum values``min_value ``=` `a_sorted[``0``]``max_value ``=` `a_sorted[``-``1``]` `# Print the results``print``(f``"min-{min_value} max-{max_value}"``)`

## C#

 `// C# code for the approach` `using` `System;` `class` `GFG {``    ``// Driver Code``    ``static` `void` `Main()``    ``{``        ``// Input array``        ``int``[] arr = { 1, 423, 6, 46, 34, 23, 13, 53, 4 };``        ``int` `n = arr.Length;``        ``// Sort the array``        ``Array.Sort(arr);` `        ``// after sorting the value at 0th position will be``        ``// minimum and nth position will be maximum``        ``Console.WriteLine(``"min-"` `+ arr[0] + ``" max-"``                          ``+ arr[n - 1]);``    ``}``}`

## Javascript

 `// JavaScript code for the approach` `// Function to find minimum and maximum values in an array``function` `findMinMax(arr) {``let min = arr[0];``let max = arr[0];` `// Iterate through the array and compare each element with current min and max``for` `(let i = 1; i < arr.length; i++) {``if` `(arr[i] < min) {``min = arr[i];``} ``else` `if` `(arr[i] > max) {``max = arr[i];``}``}` `return` `{ min, max };``}` `// Example usage``const arr = [1, 423, 6, 46, 34, 23, 13, 53, 4];``const { min, max } = findMinMax(arr);``console.log(min-\${min} max-\${max}); ``// Output: min-1 max-423`

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

## C++

 `// CPP program to find minimum (or maximum) element``// in an array.``#include ``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

 `// C program to find minimum (or maximum) element``// in an array.``#include ` `// 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

 `// 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

 `# 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.`

## 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.`

## PHP

 ``

## Javascript

 ``

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 ``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

 `// Java program to find minimum``// (or maximum) element``// in an array.``import` `java.io.*;``public` `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 */`

## Python3

 `# 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 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`

## 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 contributed by Mohit`

## Javascript

 ``

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 ``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;``}`

## Java

 `// 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

 `# 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`

## 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`

## PHP

 ``

## Javascript

 ``

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