Python program to find the largest element in an array using recursion


 

Given an array of integers, the task is to write a Program for Finding the Largest Element in an Array in Python Language using a recursion algorithm.

Input: A = [23,56,12,83,23,67,2] Output: 83

 

To do this let's understand the flow diagram:

Python Code will be:


def Max(A, n):
    if (n == 1):
        return A[0]
    return max(A[n - 1], Max(A, n - 1))
 
# Driver Code
if __name__ == "__main__":
     
    A = [23,56,12,83,23,67,2]
    n = len(A)
    print("The maximum element is:\n")
    print(Max(A, n))

Explanation:

1. Characterize a capability Max() which takes input cluster "A" and its length "n" as contentions.

2. Check if the length of the information cluster "A" is 1. if genuine return the primary component of the info exhibit.

3. Else recursively call the findMaxRec() capability and return the Largest of A[n-1] and Max(A,n-1) utilizing max() capability.

4. Introduce factors and call the Max() capability from the driver code.

Print utilizing the print() capability.

 

The output will be:



I hope you like this

Thanks


7 views0 comments

Recent Posts

See All