 # Python Program to Find Maximum and Minimum Elements in List/Array Using Recursion

In the previous article, we have discussed Python Program For Division Two Numbers Operator Without Using Division(/) Operator

Given a list and the task is to find the maximum and minimum elements in a given List using recursion in python

Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Examples:

Example1:

Input:

Given List = [1, 6, 3, 7, 8, 4]

Output:

The Maximum element in a given list [1, 6, 3, 7, 8, 4] = 8
The Minimum element in a given list [1, 6, 3, 7, 8, 4] = 1

Example2:

Input:

Given List = [20, 30, 40, 10, 50]

Output:

The Maximum element in a given list [20, 30, 40, 10, 50] = 50
The Minimum element in a given list [20, 30, 40, 10, 50] = 10

## Program to Find Maximum and Minimum Elements in List/Array Using Recursion in Python

Below are the ways to find the maximum and minimum elements in a given List using recursion in python:

### Method #1: Using Recursion (Static Input)

Approach:

• Give the list as static input and store it in a variable.
• Calculate the length of the given list and store it in another variable.
• Pass the given list and length of the given list as the arguments to the max_elemnt, min_elemnt functions.
• Create a recursive function to say max_elemnt which takes the given list and length of the given list as the arguments and returns the maximum element in a given list using recursion.
• Check if the length of the given list is 1 using the if conditional statement.
• If the statement is true, then return the first element of the list.
• Return maximum of (gven_lst[len_lst – 1], max_elemnt(gven_lst, len_lst – 1) {Recursive logic}.
• Create a recursive function to say min_elemnt which takes the given list and length of the given list as the arguments and returns the minimum element in a given list using recursion.
• Check if the length of the given list is 1 using the if conditional statement.
• If the statement is true, then return the first element of the list.
• Return minimum of (gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1) {Recursive logic}.
• Print the maximum element of the given list.
• Print the minimum element of the given list.
• The Exit of the Program.

Below is the implementation:

# Create a recursive function to say max_elemnt which takes the given list and
# length of the given list as the arguments and returns the maximum element in a
# given list using recursion.

def max_elemnt(gven_lst, len_lst):
# Check if the length of the given list is 1 using the if conditional statement.
if len_lst == 1:
# If the statement is true, then return the first element of the list.
return gven_lst
# Return maximum of (gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1)
# {Recursive logic}.
return max(gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1))

# Create a recursive function to say min_elemnt which takes the given list and
# length of the given list as the arguments and returns the minimum element in a
# given list using recursion.

def min_elemnt(gven_lst, len_lst):
# Check if the length of the given list is 1 using the if conditional statement.
if len_lst == 1:
# If the statement is true, then return the first element of the list.
return gven_lst
# Return minimum of (gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1)
# {Recursive logic}.
return min(gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1))

# Give the list as static input and store it in a variable.
gven_lst = [1, 6, 3, 7, 8, 4]
# Calculate the length of the given list and store it in another variable.
len_lst = len(gven_lst)
# Pass the given list and length of the given list as the arguments to the max_elemnt,
# min_elemnt functions.
# Print the maximum element of the given list.
print("The Maximum element in a given list",
gven_lst, "=", max_elemnt(gven_lst, len_lst))
# Print the minimum element of the given list.
print("The Minimum element in a given list",
gven_lst, "=", min_elemnt(gven_lst, len_lst))


Output:

The Maximum element in a given list [1, 6, 3, 7, 8, 4] = 8
The Minimum element in a given list [1, 6, 3, 7, 8, 4] = 1

### Method #2: Using Recursion (User Input)

Approach:

• Give the list as user input using list(),map(),input(),and split() functions.
• Store it in a variable.
• Calculate the length of the given list and store it in another variable.
• Pass the given list and length of the given list as the arguments to the max_elemnt, min_elemnt functions.
• Create a recursive function to say max_elemnt which takes the given list and length of the given list as the arguments and returns the maximum element in a given list using recursion.
• Check if the length of the given list is 1 using the if conditional statement.
• If the statement is true, then return the first element of the list.
• Return maximum of (gven_lst[len_lst – 1], max_elemnt(gven_lst, len_lst – 1) {Recursive logic}.
• Create a recursive function to say min_elemnt which takes the given list and length of the given list as the arguments and returns the minimum element in a given list using recursion.
• Check if the length of the given list is 1 using the if conditional statement.
• If the statement is true, then return the first element of the list.
• Return minimum of (gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1) {Recursive logic}.
• Print the maximum element of the given list.
• Print the minimum element of the given list.
• The Exit of the Program.

Below is the implementation:

# Create a recursive function to say max_elemnt which takes the given list and
# length of the given list as the arguments and returns the maximum element in a
# given list using recursion.

def max_elemnt(gven_lst, len_lst):
# Check if the length of the given list is 1 using the if conditional statement.
if len_lst == 1:
# If the statement is true, then return the first element of the list.
return gven_lst
# Return maximum of (gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1)
# {Recursive logic}.
return max(gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1))

# Create a recursive function to say min_elemnt which takes the given list and
# length of the given list as the arguments and returns the minimum element in a
# given list using recursion.

def min_elemnt(gven_lst, len_lst):
# Check if the length of the given list is 1 using the if conditional statement.
if len_lst == 1:
# If the statement is true, then return the first element of the list.
return gven_lst
# Return minimum of (gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1)
# {Recursive logic}.
return min(gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1))

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gven_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list and store it in another variable.
len_lst = len(gven_lst)
# Pass the given list and length of the given list as the arguments to the max_elemnt,
# min_elemnt functions.
# Print the maximum element of the given list.
print("The Maximum element in a given list",
gven_lst, "=", max_elemnt(gven_lst, len_lst))
# Print the minimum element of the given list.
print("The Minimum element in a given list",
gven_lst, "=", min_elemnt(gven_lst, len_lst))


Output:

Enter some random List Elements separated by spaces = 20 30 40 50 10
The Maximum element in a given list [20, 30, 40, 50, 10] = 50
The Minimum element in a given list [20, 30, 40, 50, 10] = 10

Dive into numerous Python Programming Language Examples for practice and get the best out of the tutorial and learn python one step at a time.