Python recursion list – Python Program to Find Maximum and Minimum Elements in List/Array Using Recursion

Python recursion list: 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, Recursive Function To Find Maximum Element In List Python, Recursive Max Function Python, Python Recursion List, Python Max Of List, Min Element In List 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[0]
     # 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[0]
    # 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[0]
     # 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[0]
    # 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

Applications On:

  1. Write A C Program To Find Maximum And Minimum Elements In Array Using Recursion
  2. Find Max Value In List Using Recursion Python
  3. Find Max Value In Array Using Recursion Python
  4. Find Max Value In List Python Recursion
  5. Find Max And Min In Array Using Recursion Python
  6. Find Max In List Python Recursion
  7. Find The Maximum Of A List Of Numbers Using Python
  8. Find Maximum And Minimum Element In An Array In Python
  9. Find Minimum Value In Array Recursion Java
  10. Recursively Find Max Value In Array
  11. Find Max In Array Python
  12. Find Max And Min In Array Using Recursion
  13. Find Max In Array Recursive
  14. Find Max Value In Array Using Recursion Java
  15. Find Max And Min In Array Using Recursion Java
  16. Recursive Function To Find Maximum Element In An Array C
  17. Program To Find Maximum And Minimum In Python
  18. Recursive Function To Find Smallest Element In An Array
  19. Recursive Algorithm To Find Maximum Element In An Array
  20. Check Number In Array Using Recursion In Python

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.