
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Minimum and Maximum Elements of Array Using Recursion 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[0] 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.
Approach used in the below program is as follows
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[0] 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[0] 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.
Finding minimum using recursion
Example
#include <iostream> using namespace std; int recforMin(int arr[], int len){ int minimum; if (len == 1){ minimum=arr[0]; 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[0]); 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
Finding minimum using recursion
Example
#include <iostream> using namespace std; int recforMax(int arr[], int len){ int maximum; if (len == 1){ maximum=arr[0]; 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[0]); 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