Recursive program to find minimum and maximum elements of array in C++
We take the integer array Arr[] as input. The goal is to find the largest and smallest elements in an array using a recursive method.
Since we are using recursion, we will iterate through the entire array until we reach length = 1 and then return A[0], which forms the base case. Otherwise, the current element is compared to the current minimum or maximum value and its value is updated recursively for subsequent elements.
Let us look at various input and output scenarios of this −
Input − Arr= {12,67,99,76,32};
Output − Maximum value in the array: 99
Explanation − Among all elements, 99 is the maximum value.
Input − Arr= {1,0,-99,9,3};
Output − Minimum value in the array: - 99
Explanation − Among all elements, -99 is the smallest value.
The method used in the following program is as follows
Find the minimum value
Take the array Arr[] as input.
The function recforMin(int arr[], int len) accepts an input array and its length, and uses recursion to return the minimum value in the array.
Get the integer variable minimum
If the current index len is 1, set minimum=arr[0] and return minimum.
Otherwise set minimum = arr[len] or the minimum value of recforMin(arr,len-1) and return it.
Finally the smallest element will be returned.
Print the result in the main function.
Find the maximum value
Take the array Arr[] as input.
The function recforMax(int arr[], int len) accepts an input array and its length and returns the maximum value in the array using recursion.
Get the integer variable maximum.
If the current index len is 1, set maximum=arr[0] and return maximum.
Otherwise set the maximum value of maximum = arr[len] or recforMax(arr,len-1) and return it.
Finally the largest element will be returned.
Print the result in the main function.
Find the minimum value 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, the following output will be generated
Minimum in the array :-89
Find the 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, the following output will be generated
Maximum in the array :-100
The above is the detailed content of Recursive program to find minimum and maximum elements of array in C++. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.

Recursion is a powerful technique that allows a function to call itself to solve a problem. In C++, a recursive function consists of two key elements: the base case (which determines when the recursion stops) and the recursive call (which breaks the problem into smaller sub-problems ). By understanding the basics and practicing practical examples such as factorial calculations, Fibonacci sequences, and binary tree traversals, you can build your recursive intuition and use it in your code with confidence.

Recursion is a technique where a function calls itself, but has the disadvantages of stack overflow and inefficiency. Alternatives include: tail-recursion optimization, where the compiler optimizes recursive calls into loops; iteration, which uses loops instead of recursion; and coroutines, which allow execution to be paused and resumed, simulating recursive behavior.

Recursive definition and optimization: Recursive: A function calls itself internally to solve difficult problems that can be decomposed into smaller sub-problems. Tail recursion: The function performs all calculations before making a recursive call, which can be optimized into a loop. Tail recursion optimization condition: recursive call is the last operation. The recursive call parameters are the same as the original call parameters. Practical example: Calculate factorial: The auxiliary function factorial_helper implements tail recursion optimization, eliminates the call stack, and improves efficiency. Calculate Fibonacci numbers: The tail recursive function fibonacci_helper uses optimization to efficiently calculate Fibonacci numbers.

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

The following techniques are available for debugging recursive functions: Check the stack traceSet debug pointsCheck if the base case is implemented correctlyCount the number of recursive callsVisualize the recursive stack

Yes, in many programming languages, arrays can be used as function parameters, and the function will perform operations on the data stored in it. For example, the printArray function in C++ can print the elements in an array, while the printArray function in Python can traverse the array and print its elements. Modifications made to the array by these functions are also reflected in the original array in the calling function.
