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++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a[] = { 1, 423, 6, 46, 34, 23, 13, 53, 4 };
int n = sizeof (a) / sizeof (a[0]);
sort(a, a + n);
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 };
Arrays.sort(a);
System.out.println( "min-" +a[ 0 ]+ " max-" +a[a.length- 1 ]);
}
}
|
Python3
import sys
a = [ 1 , 423 , 6 , 46 , 34 , 23 , 13 , 53 , 4 ]
a_sorted = sorted (a)
min_value = a_sorted[ 0 ]
max_value = a_sorted[ - 1 ]
print (f "min-{min_value} max-{max_value}" )
|
C#
using System;
class GFG {
static void Main()
{
int [] arr = { 1, 423, 6, 46, 34, 23, 13, 53, 4 };
int n = arr.Length;
Array.Sort(arr);
Console.WriteLine( "min-" + arr[0] + " max-"
+ arr[n - 1]);
}
}
|
Javascript
function findMinMax(arr) {
let min = arr[0];
let max = arr[0];
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 };
}
const arr = [1, 423, 6, 46, 34, 23, 13, 53, 4];
const { min, max } = findMinMax(arr);
console.log(min-${min} max-${max});
|
Time complexity : O(n log(n))
Auxiliary Space : O(n)
C++
#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;
}
|
C
#include <stdio.h>
int max( int num1, int num2)
{
return (num1 > num2) ? num1 : num2;
}
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;
}
|
Java
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;
}
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));
}
}
|
Python3
def getMin(arr, n):
res = arr[ 0 ]
for i in range ( 1 ,n):
res = min (res, arr[i])
return res
def getMax(arr, n):
res = arr[ 0 ]
for i in range ( 1 ,n):
res = max (res, arr[i])
return res
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))
|
C#
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;
}
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));
}
}
|
PHP
<?php
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 ;
}
$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 );
?>
|
Javascript
<script>
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;
}
let 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));
</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
Recursive Solution
C++
#include <bits/stdc++.h>
using namespace std;
int getMin( int arr[], int n)
{
return (n == 1) ? arr[0] : min(arr[0],
getMin(arr + 1, n - 1));
}
int getMax( int arr[], int n)
{
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
import java.io.*;
public class GFG
{
static int getMin( int arr[], int i, int n)
{
return (n == 1 ) ? arr[i] : Math.min(arr[i],
getMin(arr,i + 1 , n - 1 ));
}
static int getMax( int arr[], int i, int n)
{
return (n == 1 ) ? arr[i] : Math.max(arr[i],
getMax(arr ,i + 1 , n - 1 ));
}
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));
}
}
|
Python3
def getMin(arr, n):
if (n = = 1 ):
return arr[ 0 ]
else :
return min (getMin(arr[ 1 :], n - 1 ), arr[ 0 ])
def getMax(arr, n):
if (n = = 1 ):
return arr[ 0 ]
else :
return max (getMax(arr[ 1 :], n - 1 ), arr[ 0 ])
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));
|
C#
using System;
class GFG
{
static int getMin( int []arr, int i, int n)
{
return (n == 1) ? arr[i] : Math.Min(arr[i],
getMin(arr,i + 1 , n - 1));
}
static int getMax( int []arr, int i, int n)
{
return (n == 1) ? arr[i] : Math.Max(arr[i],
getMax(arr ,i + 1, n - 1));
}
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));
}
}
|
Javascript
<script>
function getMin(arr , i , n) {
return (n == 1) ? arr[i] : Math.min(arr[i], getMin(arr, i + 1, n - 1));
}
function getMax(arr , i , n) {
return (n == 1) ? arr[i] : Math.max(arr[i], getMax(arr, i + 1, n - 1));
}
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));
</script>
|
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++
#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;
}
|
Java
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();
}
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));
}
}
|
Python3
def getMin(arr,n):
return min (arr)
def getMax(arr,n):
return max (arr)
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))
|
C#
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();
}
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));
}
}
|
PHP
<?php
function getMin(& $arr , $n )
{
return min( $arr );
}
function getMax(& $arr , $n )
{
return max( $arr );
}
$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 );
?>
|
Javascript
<script>
function getMin(arr , n)
{
return Math.min.apply(Math,arr);
}
function getMax(arr , n) {
return Math.max.apply(Math,arr);
}
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));
</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.
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!