Program to Find Elements Larger than Half of the Elements in an ArrayList

Python Program to Find Elements Larger than Half of the Elements in an Array/List

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 floor(n/2) where n is the total number of elements in the given list.

Examples:

Example1:

Input:

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

Output:

The elements in a list that are greater than half of the total number of elements in the given list
6
8
9

Explanation:

Here the elements which are greater than 4 are 6,8,9

Example2:

Input:

Given List =  [1, 0, 6, 9, 3, 4, 2, 6, 3]

Output:

The elements in a list that are greater than half of the total number of elements in the given list
3
4
6
6
9

Program to Find Elements Larger than Half of the Elements in an Array/List in Python

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:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Pass the given list and length of the given list as the arguments to the getElementsLarger() function.
  • Create a function to say getElementsLarger() which takes the given list and length of the given list as the arguments and prints the elements in a list that are greater than half of the total number of elements in the given list.
  • Inside the function, sort the given list using the sorted() function and store it in a variable.
  • Loop from half the length of the given list to the length of the given list-1 using the for loop.
  • Inside the loop, print the element present at the iterator value of the above-sorted list.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say getElementsLarger() which takes the given list and length of
# the given list as the arguments and prints the elements in a list that are greater than
# half of the total number of elements in the given list.


def getElementsLarger(gvn_lst, lengt_lst):
        # Inside the function, sort the given list using the sorted() function and store
        # it in a variable.
    k = sorted(gvn_lst)
    # Loop from half the length of the given list to the length of the given list-1
    # using the for loop.
    for itr in range(lengt_lst//2, lengt_lst):
     # Inside the loop, print the element present at the iterator value of the
     # above-sorted list.

        print(k[itr])


# Give the list as static input and store it in a variable.
gvn_lst = [4, 6, 3, 1, 8, 9]
# Calculate the length of the given list using the len() function and store it in
# another variable.
lengt_lst = len(gvn_lst)
print("The elements in a list that are greater than half of the total number of elements in the given list")
# Pass the given list and length of the given list as the arguments to the
# getElementsLarger() function.
getElementsLarger(gvn_lst, lengt_lst)

Output:

The elements in a list that are greater than half of the total number of elements in the given list
6
8
9

Method #2: Using For loop (User Input)

Approach:

  • Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Pass the given list and length of the given list as the arguments to the getElementsLarger() function.
  • Create a function to say getElementsLarger() which takes the given list and length of the given list as the arguments and prints the elements in a list that are greater than half of the total number of elements in the given list.
  • Inside the function, sort the given list using the sorted() function and store it in a variable.
  • Loop from half the length of the given list to the length of the given list-1 using the for loop.
  • Inside the loop, print the element present at the iterator value of the above-sorted list.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say getElementsLarger() which takes the given list and length of
# the given list as the arguments and prints the elements in a list that are greater than
# half of the total number of elements in the given list.


def getElementsLarger(gvn_lst, lengt_lst):
        # Inside the function, sort the given list using the sorted() function and store
        # it in a variable.
    k = sorted(gvn_lst)
    # Loop from half the length of the given list to the length of the given list-1
    # using the for loop.
    for itr in range(lengt_lst//2, lengt_lst):
     # Inside the loop, print the element present at the iterator value of the
     # above-sorted list.

        print(k[itr])


# Give the list as user input using the list(),map(),split(),int functions and 
# store it in a variable.
gvn_lst = list( map(int, input('Enter some random list element separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and store it in
# another variable.
lengt_lst = len(gvn_lst)
print("The elements in a list that are greater than half of the total number of elements in the given list")
# Pass the given list and length of the given list as the arguments to the
# getElementsLarger() function.
getElementsLarger(gvn_lst, lengt_lst)

Output:

Enter some random list element separated by spaces = 10 20 45 23 45 10
The elements in a list that are greater than half of the total number of elements in the given list
23
45
45

Grab the opportunity and utilize the Python Program Code Examples over here to prepare basic and advanced topics too with ease and clear all your doubts.