# Recursive Programs to find Minimum and Maximum elements of array in C++

We are given an integer array Arr[] as input. The goal is to find maximum and minimum elements among the array using recursive methods.

Since we are using recursion, we will traverse the whole array till we reach length=1 then return A which forms the base case. Else compare current element with present minimum or maximum and update its value by recursion for later elements.

## Let us see various input output scenarios for this −

Input − Arr= {12,67,99,76,32};

Output − Maximum in the array :99

Explanation − Out of all elements 99 is maximum among them.

Input − Arr= {1,0,-99,9,3};

Output − Minimum in the array :-99

Explanation − Out of all elements -99 is minimum among them.

## For finding Minimum

• Take array Arr[] as input.

• Function recforMin(int arr[], int len) takes input array and its length and returns minimum in the array using recursion.

• Take the integer variable minimum

• If the current index len is 1 then set minimum=arr and return minimum.

• Else set minimum = minimum of arr[len] or recforMin(arr,len-1) and return it.

• At the end the minimum element will be returned.

• Print result obtained inside main.

## For finding Minimum

• For finding Maximum

• Take array Arr[] as input.

• Function recforMax(int arr[], int len) takes input array and its length and returns maximum in the array using recursion.

• Take the integer variable maximum.

• If the current index len is 1 then set maximum=arr and return maximum.

• Else set minimum = maximum of arr[len] or recforMax(arr,len-1) and return it.

• At the end the maximum element will be returned.

• Print result obtained inside main.

## Example

#include <iostream>
using namespace std;
int recforMin(int arr[], int len){
int minimum;
if (len == 1){
minimum=arr;
return minimum;
}
else{
return minimum=arr[len]<recforMin(arr,len-1)?arr[len]:recforMin(arr,len-1);
}
}
int main(){
int Arr[] = {-89,98,76,32,21,35,100};
int length = sizeof(Arr)/sizeof(Arr);
cout <<"Minimum in the array :"<<recforMin(Arr, length);
return 0;
}

## Output

If we run the above code it will generate the following Output

Minimum in the array :-89

## Example

#include <iostream>
using namespace std;
int recforMax(int arr[], int len){
int maximum;

if (len == 1){
maximum=arr;
return maximum;
}
else{
return maximum=arr[len]>recforMax(arr,len-1)?arr[len]:recforMax(arr,len-1);
}
}
int main(){
int Arr[] = {-89,98,76,32,21,35,100};
int length = sizeof(Arr)/sizeof(Arr);
cout <<"Maximum in the array :"<<recforMax(Arr, length);
return 0;
}

## Output

If we run the above code it will generate the following Output

Maximum in the array :-100