
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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[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
- Related Articles
- Returning an array with the minimum and maximum elements JavaScript
- C# program to find maximum and minimum element in an array\n
- C++ Program to find pairs of sequences where sequence holds minimum and maximum elements
- Find minimum and maximum elements in singly Circular Linked List in C++
- How to find the minimum and maximum element of an Array using STL in C++?
- Maximum and minimum of an array using minimum number of comparisons in C
- Program to find the minimum (or maximum) element of an array in C++
- Maximum and Minimum K elements in Tuple using Python
- JavaScript Program to Find k maximum elements of array in original order
- 8085 program to find maximum and minimum of 10 numbers
- C Program to Minimum and Maximum prime numbers in an array
- Find k maximum elements of array in original order in C++
- Product of maximum in first array and minimum in second in C
- Find the first, second and third minimum elements in an array in C++
- Counting elements of an array using a recursive function in JS?
