{"id":21506,"date":"2021-09-22T17:36:43","date_gmt":"2021-09-22T12:06:43","guid":{"rendered":"https:\/\/python-programs.com\/?p=21506"},"modified":"2021-11-22T18:36:12","modified_gmt":"2021-11-22T13:06:12","slug":"python-program-to-find-elements-larger-than-half-of-the-elements-in-an-array-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-elements-larger-than-half-of-the-elements-in-an-array-list\/","title":{"rendered":"Python Program to Find Elements Larger than Half of the Elements in an Array\/List"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Sort the Given Matrix<\/a><\/p>\n Given a list and the task is to find the elements in a list that are greater than half of the total number of elements in the given list.<\/p>\n In the case of odd elements, we must print elements greater than floor(n\/2) where n is the total number of elements in the given list.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation<\/strong>:<\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the elements in a list that are greater than half of the total number of elements in the given list:<\/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 Grab the opportunity and utilize the Python Program Code Examples<\/a> over here to prepare basic and advanced topics too with ease and clear all your doubts.<\/p>\n In the previous article, we have discussed Python Program to Sort the Given Matrix Given a list and the task is to find the elements in a list that are greater than half of the total number of elements in the given list. In the case of odd elements, we must print elements greater than …<\/p>\nGiven List = [4, 6, 3, 1, 8, 9]<\/pre>\n
The elements in a list that are greater than half of the total number of elements in the given list\r\n6\r\n8\r\n9<\/pre>\n
Here the elements which are greater than 4 are 6,8,9<\/pre>\n
Given List = [1, 0, 6, 9, 3, 4, 2, 6, 3]<\/pre>\n
The elements in a list that are greater than half of the total number of elements in the given list\r\n3\r\n4\r\n6\r\n6\r\n9<\/pre>\n
Program to Find Elements Larger than Half of the Elements in an Array\/List in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function to say getElementsLarger() which takes the given list and length of\r\n# the given list as the arguments and prints the elements in a list that are greater than\r\n# half of the total number of elements in the given list.\r\n\r\n\r\ndef getElementsLarger(gvn_lst, lengt_lst):\r\n # Inside the function, sort the given list using the sorted() function and store\r\n # it in a variable.\r\n k = sorted(gvn_lst)\r\n # Loop from half the length of the given list to the length of the given list-1\r\n # using the for loop.\r\n for itr in range(lengt_lst\/\/2, lengt_lst):\r\n # Inside the loop, print the element present at the iterator value of the\r\n # above-sorted list.\r\n\r\n print(k[itr])\r\n\r\n\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [4, 6, 3, 1, 8, 9]\r\n# Calculate the length of the given list using the len() function and store it in\r\n# another variable.\r\nlengt_lst = len(gvn_lst)\r\nprint(\"The elements in a list that are greater than half of the total number of elements in the given list\")\r\n# Pass the given list and length of the given list as the arguments to the\r\n# getElementsLarger() function.\r\ngetElementsLarger(gvn_lst, lengt_lst)\r\n<\/pre>\n
The elements in a list that are greater than half of the total number of elements in the given list\r\n6\r\n8\r\n9<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Create a function to say getElementsLarger() which takes the given list and length of\r\n# the given list as the arguments and prints the elements in a list that are greater than\r\n# half of the total number of elements in the given list.\r\n\r\n\r\ndef getElementsLarger(gvn_lst, lengt_lst):\r\n # Inside the function, sort the given list using the sorted() function and store\r\n # it in a variable.\r\n k = sorted(gvn_lst)\r\n # Loop from half the length of the given list to the length of the given list-1\r\n # using the for loop.\r\n for itr in range(lengt_lst\/\/2, lengt_lst):\r\n # Inside the loop, print the element present at the iterator value of the\r\n # above-sorted list.\r\n\r\n print(k[itr])\r\n\r\n\r\n# Give the list as user input using the list(),map(),split(),int functions and \r\n# store it in a variable.\r\ngvn_lst = list( map(int, input('Enter some random list element separated by spaces = ').split()))\r\n# Calculate the length of the given list using the len() function and store it in\r\n# another variable.\r\nlengt_lst = len(gvn_lst)\r\nprint(\"The elements in a list that are greater than half of the total number of elements in the given list\")\r\n# Pass the given list and length of the given list as the arguments to the\r\n# getElementsLarger() function.\r\ngetElementsLarger(gvn_lst, lengt_lst)\r\n<\/pre>\n
Enter some random list element separated by spaces = 10 20 45 23 45 10\r\nThe elements in a list that are greater than half of the total number of elements in the given list\r\n23\r\n45\r\n45<\/pre>\n
\n