{"id":20410,"date":"2021-09-11T15:38:14","date_gmt":"2021-09-11T10:08:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=20410"},"modified":"2021-11-22T18:36:21","modified_gmt":"2021-11-22T13:06:21","slug":"python-program-to-find-maximum-and-minimum-elements-in-list-array-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-maximum-and-minimum-elements-in-list-array-using-recursion\/","title":{"rendered":"Python Program to Find Maximum and Minimum Elements in List\/Array Using Recursion"},"content":{"rendered":"
In the previous article, we have discussed Python Program For Division Two Numbers Operator Without Using Division(\/) Operator<\/a><\/p>\n Given a list and the task is to find the maximum and minimum elements in a given List using recursion in python<\/p>\n Recursion:<\/strong><\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the maximum and minimum elements in a given List using recursion in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Dive into numerous Python Programming Language Examples<\/a> for practice and get the best out of the tutorial and learn python one step at a time.<\/p>\n 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 …<\/p>\nGiven List = [1, 6, 3, 7, 8, 4]<\/pre>\n
The Maximum element in a given list [1, 6, 3, 7, 8, 4] = 8\r\nThe Minimum element in a given list [1, 6, 3, 7, 8, 4] = 1<\/pre>\n
Given List = [20, 30, 40, 10, 50]<\/pre>\n
The Maximum element in a given list [20, 30, 40, 10, 50] = 50\r\nThe Minimum element in a given list [20, 30, 40, 10, 50] = 10<\/pre>\n
Program to Find Maximum and Minimum Elements in List\/Array Using Recursion in Python<\/h2>\n
\n
Method #1: Using Recursion (Static Input)<\/h3>\n
\n
# Create a recursive function to say max_elemnt which takes the given list and\r\n# length of the given list as the arguments and returns the maximum element in a\r\n# given list using recursion.\r\n\r\n\r\ndef max_elemnt(gven_lst, len_lst):\r\n # Check if the length of the given list is 1 using the if conditional statement.\r\n if len_lst == 1:\r\n # If the statement is true, then return the first element of the list.\r\n return gven_lst[0]\r\n # Return maximum of (gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1)\r\n # {Recursive logic}.\r\n return max(gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1))\r\n\r\n# Create a recursive function to say min_elemnt which takes the given list and\r\n# length of the given list as the arguments and returns the minimum element in a\r\n# given list using recursion.\r\n\r\n\r\ndef min_elemnt(gven_lst, len_lst):\r\n # Check if the length of the given list is 1 using the if conditional statement.\r\n if len_lst == 1:\r\n # If the statement is true, then return the first element of the list.\r\n return gven_lst[0]\r\n # Return minimum of (gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1)\r\n # {Recursive logic}.\r\n return min(gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1))\r\n\r\n\r\n# Give the list as static input and store it in a variable.\r\ngven_lst = [1, 6, 3, 7, 8, 4]\r\n# Calculate the length of the given list and store it in another variable.\r\nlen_lst = len(gven_lst)\r\n# Pass the given list and length of the given list as the arguments to the max_elemnt,\r\n# min_elemnt functions.\r\n# Print the maximum element of the given list.\r\nprint(\"The Maximum element in a given list\",\r\n gven_lst, \"=\", max_elemnt(gven_lst, len_lst))\r\n# Print the minimum element of the given list.\r\nprint(\"The Minimum element in a given list\",\r\n gven_lst, \"=\", min_elemnt(gven_lst, len_lst))\r\n<\/pre>\n
The Maximum element in a given list [1, 6, 3, 7, 8, 4] = 8\r\nThe Minimum element in a given list [1, 6, 3, 7, 8, 4] = 1<\/pre>\n
Method #2: Using Recursion (User Input)<\/h3>\n
\n
# Create a recursive function to say max_elemnt which takes the given list and\r\n# length of the given list as the arguments and returns the maximum element in a\r\n# given list using recursion.\r\n\r\n\r\ndef max_elemnt(gven_lst, len_lst):\r\n # Check if the length of the given list is 1 using the if conditional statement.\r\n if len_lst == 1:\r\n # If the statement is true, then return the first element of the list.\r\n return gven_lst[0]\r\n # Return maximum of (gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1)\r\n # {Recursive logic}.\r\n return max(gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1))\r\n\r\n# Create a recursive function to say min_elemnt which takes the given list and\r\n# length of the given list as the arguments and returns the minimum element in a\r\n# given list using recursion.\r\n\r\n\r\ndef min_elemnt(gven_lst, len_lst):\r\n # Check if the length of the given list is 1 using the if conditional statement.\r\n if len_lst == 1:\r\n # If the statement is true, then return the first element of the list.\r\n return gven_lst[0]\r\n # Return minimum of (gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1)\r\n # {Recursive logic}.\r\n return min(gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1))\r\n\r\n\r\n\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngven_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split()))\r\n# Calculate the length of the given list and store it in another variable.\r\nlen_lst = len(gven_lst)\r\n# Pass the given list and length of the given list as the arguments to the max_elemnt,\r\n# min_elemnt functions.\r\n# Print the maximum element of the given list.\r\nprint(\"The Maximum element in a given list\",\r\n gven_lst, \"=\", max_elemnt(gven_lst, len_lst))\r\n# Print the minimum element of the given list.\r\nprint(\"The Minimum element in a given list\",\r\n gven_lst, \"=\", min_elemnt(gven_lst, len_lst))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 20 30 40 50 10\r\nThe Maximum element in a given list [20, 30, 40, 50, 10] = 50\r\nThe Minimum element in a given list [20, 30, 40, 50, 10] = 10<\/pre>\n
\n