Recursive Programs to find Minimum and Maximum elements of array
Last Updated :
19 Sep, 2023
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++
// Recursive C++ program to find minimum
#include <iostream>
using namespace std;
// function to print Minimum element using recursion
int findMinRec(int A[], int n)
{
// if size = 0 means whole array has been traversed
if (n == 1)
return A[0];
return min(A[n-1], findMinRec(A, n-1));
}
// driver code to test above function
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
// Recursive Java program to find minimum
import java.util.*;
class GFG {
// function to return minimum element using recursion
public static int findMinRec(int A[], int n)
{
// if size = 0 means whole array
// has been traversed
if(n == 1)
return A[0];
return Math.min(A[n-1], findMinRec(A, n-1));
}
// Driver code
public static void main(String args[])
{
int A[] = {1, 4, 45, 6, -50, 10, 2};
int n = A.length;
// Function calling
System.out.println(findMinRec(A, n));
}
}
//This code is contributed by Niraj_Pandey
Python3
# Recursive python 3 program to
# find minimum
# function to print Minimum element
# using recursion
def findMinRec(A, n):
# if size = 0 means whole array
# has been traversed
if (n == 1):
return A[0]
return min(A[n - 1], findMinRec(A, n - 1))
# Driver Code
if __name__ == '__main__':
A = [1, 4, 45, 6, -50, 10, 2]
n = len(A)
print(findMinRec(A, n))
# This code is contributed by
# Shashank_Sharma
C#
// Recursive C# program to find minimum
using System;
class GFG
{
// function to return minimum
// element using recursion
public static int findMinRec(int []A,
int n)
{
// if size = 0 means whole array
// has been traversed
if(n == 1)
return A[0];
return Math.Min(A[n - 1],
findMinRec(A, n - 1));
}
// Driver code
static public void Main ()
{
int []A = {1, 4, 45, 6, -50, 10, 2};
int n = A.Length;
// Function calling
Console.WriteLine(findMinRec(A, n));
}
}
// This code is contributed by Sachin
PHP
<?php
// Recursive PHP program to find minimum
// function to print Minimum
// element using recursion
function findMinRec($A, $n)
{
// if size = 0 means whole
// array has been traversed
if ($n == 1)
return $A[0];
return min($A[$n - 1], findMinRec($A, $n - 1));
}
// Driver Code
$A = array (1, 4, 45, 6, -50, 10, 2);
$n = sizeof($A);
echo findMinRec($A, $n);
// This code is contributed by akt
?>
JavaScript
<script>
// Javascript program to find minimum
// Function to print Minimum
// element using recursion
function findMinRec(A, n)
{
// If size = 0 means whole
// array has been traversed
if (n == 1)
return A[0];
return Math.min(A[n - 1],
findMinRec(A, n - 1));
}
// Driver Code
let A = [ 1, 4, 45, 6, -50, 10, 2 ];
let n = A.length;
document.write( findMinRec(A, n));
// This code is contributed by sravan kumar G
</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++
// Recursive C++ program to find maximum
#include <iostream>
using namespace std;
// function to return maximum element using recursion
int findMaxRec(int A[], int n)
{
// if n = 0 means whole array has been traversed
if (n == 1)
return A[0];
return max(A[n-1], findMaxRec(A, n-1));
}
// driver code to test above function
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
// Recursive Java program to find maximum
import java.util.*;
class GFG {
// function to return maximum element using recursion
public static int findMaxRec(int A[], int n)
{
// if size = 0 means whole array
// has been traversed
if(n == 1)
return A[0];
return Math.max(A[n-1], findMaxRec(A, n-1));
}
// Driver code
public static void main(String args[])
{
int A[] = {1, 4, 45, 6, -50, 10, 2};
int n = A.length;
// Function calling
System.out.println(findMaxRec(A, n));
}
}
//This code is contributed by Niraj_Pandey
Python3
# Recursive Python 3 program to
# find maximum
# function to return maximum element
# using recursion
def findMaxRec(A, n):
# if n = 0 means whole array
# has been traversed
if (n == 1):
return A[0]
return max(A[n - 1], findMaxRec(A, n - 1))
# Driver Code
if __name__ == "__main__":
A = [1, 4, 45, 6, -50, 10, 2]
n = len(A)
print(findMaxRec(A, n))
# This code is contributed by ita_c
C#
// Recursive C# program to find maximum
using System;
class GFG
{
// function to return maximum
// element using recursion
public static int findMaxRec(int []A,
int n)
{
// if size = 0 means whole array
// has been traversed
if(n == 1)
return A[0];
return Math.Max(A[n - 1],
findMaxRec(A, n - 1));
}
// Driver code
static public void Main ()
{
int []A = {1, 4, 45, 6, -50, 10, 2};
int n = A.Length;
// Function calling
Console.WriteLine(findMaxRec(A, n));
}
}
// This code is contributed by Sach_Code
PHP
<?php
// Recursive PHP program to find maximum
// function to return maximum
// element using recursion
function findMaxRec($A, $n)
{
// if n = 0 means whole array
// has been traversed
if ($n == 1)
return $A[0];
return max($A[$n - 1],
findMaxRec($A, $n - 1));
}
// Driver Code
$A = array(1, 4, 45, 6, -50, 10, 2);
$n = sizeof($A);
echo findMaxRec($A, $n);
// This code is contributed
// by Akanksha Rai
?>
JavaScript
<script>
// Recursive Java program to find maximum
// Function to return maximum element
// using recursion
function findMaxRec(A, n)
{
// If size = 0 means whole array
// has been traversed
if (n == 1)
return A[0];
return Math.max(A[n - 1],
findMaxRec(A, n - 1));
}
// Driver code
let A = [ 1, 4, 45, 6, -50, 10, 2 ];
let n = A.length;
// Function calling
document.write(findMaxRec(A, n));
// This code is contributed by sravan kumar G
</script>
Related article:
Program to find largest element in an array
Similar Reads
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. Examples:Input: arr[] = [1, 423, 6, 46, 34, 23, 13, 53, 4]Output: Minimum element of array: 1 Maximum element of array: 423Input: arr[] = [2, 4, 6, 7, 9, 8, 3, 11]Output: Minimum element of array: 2 Maximum element of ar
14 min read
Find the minimum and maximum sum of N-1 elements of the array
Given an unsorted array A of size N, the task is to find the minimum and maximum values that can be calculated by adding exactly N-1 elements. Examples: Input: a[] = {13, 5, 11, 9, 7} Output: 32 40 Explanation: Minimum sum is 5 + 7 + 9 + 11 = 32 and maximum sum is 7 + 9 + 11 + 13 = 40.Input: a[] = {
10 min read
Maximize minimum element of an Array using operations
Given an array A[] of N integers and two integers X and Y (X ⤠Y), the task is to find the maximum possible value of the minimum element in an array A[] of N integers by adding X to one element and subtracting Y from another element any number of times, where X ⤠Y. Examples: Input: N= 3, A[] = {1,
8 min read
Maximum of minimums of every window size in a given array
Given an integer array arr[] of size n, the task is to find the maximum of the minimums for every window size in the given array, where the window size ranges from 1 to n.Example:Input: arr[] = [10, 20, 30]Output: [30, 20, 10]Explanation: First element in output indicates maximum of minimums of all
14 min read
Sum and Product of minimum and maximum element of an Array
Given an array. The task is to find the sum and product of the maximum and minimum elements of the given array. Examples: Input : arr[] = {12, 1234, 45, 67, 1} Output : Sum = 1235 Product = 1234 Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9} Output : Sum = 10 Product = 9 Take two variables min and max to
13 min read
Leftmost and rightmost indices of the maximum and the minimum element of an array
Given an array arr[], the task is to find the leftmost and the rightmost indices of the minimum and the maximum element from the array where arr[] consists of non-distinct elements.Examples: Input: arr[] = {2, 1, 1, 2, 1, 5, 6, 5} Output: Minimum left : 1 Minimum right : 4 Maximum left : 6 Maximum r
15+ min read
Find the Array element left after alternate removal of minimum and maximum
Given an array Arr of length N, it is reduced by 1 element at each step. Maximum and Minimum elements will be removed in alternating order from the remaining array until a single element is remaining in the array. The task is to find the remaining element in the given array. Examples: Input: arr[] =
4 min read
Find the number of operations required to make all array elements Equal
Given an array of N integers, the task is to find the number of operations required to make all elements in the array equal. In one operation we can distribute equal weights from the maximum element to the rest of the array elements. If it is not possible to make the array elements equal after perfo
6 min read
Minimum index of element with maximum multiples in Array
Given an array arr[] of length n, the task is to calculate the min index of the element which has maximum elements present in the form of {2 * arr[i], 3 * arr[i], 4 * arr[i], 5 * arr[i]}. Examples: Input: n = 4. arr[] = {2, 1, 3, 6}Output: 1Explanation: For 2, {2*2, 3*2, 4*2, 5*2} => {4, 6, 8, 10
5 min read
Maximum sum of minimums of pairs in an array
Given an array arr[] of N integers where N is even, the task is to group the array elements in the pairs (X1, Y1), (X2, Y2), (X3, Y3), ... such that the sum min(X1, Y1) + min(X2, Y2) + min(X3, Y3) + ... is maximum.Examples: Input: arr[] = {1, 5, 3, 2} Output: 4 (1, 5) and (3, 2) -> 1 + 2 = 3 (1,
4 min read