Recursive max function

Recursive function to find max in array

Write a recursive function to find the maximum value element in an Array of size N.

Recursive max function

Recursive definition of maximum

In order to solve this problem recursively we need a stopping condition to break the chain of recursive calls. We also need to represent the solution for an array of size N in terms of an array of size N-1. The stopping condition is met when the input array has only one element. In that case we just return it as the maximum. Otherwise we compare one element of the array to the maximum element of the rest of the array. We return this element if it is greater otherwise we return the maximum of the rest of the array.

Recursive max algorithm

The following is a suggested solution in PHP.

If you have questions or comments, please use the comments section below .Thanks for visiting.

Add a Comment

Your email address will not be published. Required fields are marked *