Recursive Programs to find Minimum and Maximum elements of array
Given an array of integers arr, the task is to find the minimum and maximum element of that array using recursion.
Examples :
Input: arr = {1, 4, 3, -5, -4, 8, 6};
Output: min = -5, max = 8
Input: arr = {1, 4, 45, 6, 10, -8};
Output: min = -8, max = 45
Recursive approach to find the Minimum element in the array
Approach:
- Get the array for which the minimum is to be found
- Recursively find the minimum according to the following:
- Recursively traverse the array from the end
- Base case: If the remaining array is of length 1, return the only present element i.e. arr[0]
if(n == 1)
return arr[0];
- Recursive call: If the base case is not met, then call the function by passing the array of one size less from the end, i.e. from arr[0] to arr[n-1].
- Return statement: At each recursive call (except for the base case), return the minimum of the last element of the current array (i.e. arr[n-1]) and the element returned from the previous recursive call.
return min(arr[n-1], recursive_function(arr, n-1));
- Print the returned element from the recursive function as the minimum element
Pseudocode for Recursive function:
If there is single element, return it.
Else return minimum of following.
a) Last Element
b) Value returned by recursive call
for n-1 elements.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int findMinRec( int A[], int n)
{
if (n == 1)
return A[0];
return min(A[n-1], findMinRec(A, n-1));
}
int main()
{
int A[] = {1, 4, 45, 6, -50, 10, 2};
int n = sizeof (A)/ sizeof (A[0]);
cout << findMinRec(A, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int findMinRec( int A[], int n)
{
if (n == 1 )
return A[ 0 ];
return Math.min(A[n- 1 ], findMinRec(A, n- 1 ));
}
public static void main(String args[])
{
int A[] = { 1 , 4 , 45 , 6 , - 50 , 10 , 2 };
int n = A.length;
System.out.println(findMinRec(A, n));
}
}
|
Python3
def findMinRec(A, n):
if (n = = 1 ):
return A[ 0 ]
return min (A[n - 1 ], findMinRec(A, n - 1 ))
if __name__ = = '__main__' :
A = [ 1 , 4 , 45 , 6 , - 50 , 10 , 2 ]
n = len (A)
print (findMinRec(A, n))
|
C#
using System;
class GFG
{
public static int findMinRec( int []A,
int n)
{
if (n == 1)
return A[0];
return Math.Min(A[n - 1],
findMinRec(A, n - 1));
}
static public void Main ()
{
int []A = {1, 4, 45, 6, -50, 10, 2};
int n = A.Length;
Console.WriteLine(findMinRec(A, n));
}
}
|
PHP
<?php
function findMinRec( $A , $n )
{
if ( $n == 1)
return $A [0];
return min( $A [ $n - 1], findMinRec( $A , $n - 1));
}
$A = array (1, 4, 45, 6, -50, 10, 2);
$n = sizeof( $A );
echo findMinRec( $A , $n );
?>
|
Javascript
<script>
function findMinRec(A, n)
{
if (n == 1)
return A[0];
return Math.min(A[n - 1],
findMinRec(A, n - 1));
}
let A = [ 1, 4, 45, 6, -50, 10, 2 ];
let n = A.length;
document.write( findMinRec(A, n));
</script>
|
Recursive approach to find the Maximum element in the array
Approach:
- Get the array for which the maximum is to be found
- Recursively find the maximum according to the following:
- Recursively traverse the array from the end
- Base case: If the remaining array is of length 1, return the only present element i.e. arr[0]
if(n == 1)
return arr[0];
- Recursive call: If the base case is not met, then call the function by passing the array of one size less from the end, i.e. from arr[0] to arr[n-1].
- Return statement: At each recursive call (except for the base case), return the maximum of the last element of the current array (i.e. arr[n-1]) and the element returned from the previous recursive call.
return max(arr[n-1], recursive_function(arr, n-1));
- Print the returned element from the recursive function as the maximum element
Pseudocode for Recursive function:
If there is single element, return it.
Else return maximum of following.
a) Last Element
b) Value returned by recursive call
for n-1 elements.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int findMaxRec( int A[], int n)
{
if (n == 1)
return A[0];
return max(A[n-1], findMaxRec(A, n-1));
}
int main()
{
int A[] = {1, 4, 45, 6, -50, 10, 2};
int n = sizeof (A)/ sizeof (A[0]);
cout << findMaxRec(A, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int findMaxRec( int A[], int n)
{
if (n == 1 )
return A[ 0 ];
return Math.max(A[n- 1 ], findMaxRec(A, n- 1 ));
}
public static void main(String args[])
{
int A[] = { 1 , 4 , 45 , 6 , - 50 , 10 , 2 };
int n = A.length;
System.out.println(findMaxRec(A, n));
}
}
|
Python3
def findMaxRec(A, n):
if (n = = 1 ):
return A[ 0 ]
return max (A[n - 1 ], findMaxRec(A, n - 1 ))
if __name__ = = "__main__" :
A = [ 1 , 4 , 45 , 6 , - 50 , 10 , 2 ]
n = len (A)
print (findMaxRec(A, n))
|
C#
using System;
class GFG
{
public static int findMaxRec( int []A,
int n)
{
if (n == 1)
return A[0];
return Math.Max(A[n - 1],
findMaxRec(A, n - 1));
}
static public void Main ()
{
int []A = {1, 4, 45, 6, -50, 10, 2};
int n = A.Length;
Console.WriteLine(findMaxRec(A, n));
}
}
|
PHP
<?php
function findMaxRec( $A , $n )
{
if ( $n == 1)
return $A [0];
return max( $A [ $n - 1],
findMaxRec( $A , $n - 1));
}
$A = array (1, 4, 45, 6, -50, 10, 2);
$n = sizeof( $A );
echo findMaxRec( $A , $n );
?>
|
Javascript
<script>
function findMaxRec(A, n)
{
if (n == 1)
return A[0];
return Math.max(A[n - 1],
findMaxRec(A, n - 1));
}
let A = [ 1, 4, 45, 6, -50, 10, 2 ];
let n = A.length;
document.write(findMaxRec(A, n));
</script>
|
Related article:
Program to find largest element in an array
Last Updated :
19 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...