Program to Find Leaders in an ArrayList

Python Program to Find Leaders in an Array/List

In the previous article, we have discussed Python Program to Check Automorphic Number or Not
Leader:

If an element is greater than all of the elements on its right side, it is the leader. And the last element is always a leader.

Examples:

Example1:

Input:

Given list =[23, 11, 1, 7, 8, 6, 3]

Output:

The leaders of the given list [23, 11, 1, 7, 8, 6, 3] are :
23
11
8
6
3

Example2:

Input:

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

Output:

The leaders of the given list [1, 2, 3, 7, 8, 6] are :
8
6

Given a list, the task is to find all the leaders of the given list in python.

Program to Find Leaders in an Array/List in Python

Below are the ways to find all the leaders of the given list in python some of them are:

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 list and store it in another variable.
  • Loop from 0 to the length of the list using the For loop.
  • Loop from parent loop iterator value to the length of the list using another Nested for loop(Inner For loop)
  • Check if the element at the index of the parent loop iterator value is less than or equal to the element at the index of the inner loop iterator value.(gvnlist [ m ] <= gvnlist [ n ] ) using the if conditional statement.
  • If it is true then break the inner loop using the break keyword.
  • After the end of the inner For loop check if the inner loop iterator value is equal to the length of the given list -1 using the if conditional statement.
  • If it is true then it is the leader so print it.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvnlstt = [23, 11, 1, 7, 8, 6, 3]
# Calculate the length of the list and store it in another variable.
lstleng = len(gvnlstt)
print('The leaders of the given list', gvnlstt, 'are :')
# Loop from 0 to the length of the list using the For loop.
for m in range(lstleng):
    # Loop from parent loop iterator value to the length of the list
    # using another Nested for loop(Inner For loop)
    for n in range(m+1, lstleng):
            # Check if the element at the index of the parent loop iterator value
        # is less than or equal to the element at the index of the inner loop iterator value.
        # (gvnlist[m] <= gvnlist[n]) using the if conditional statement.
        if (gvnlstt[m] <= gvnlstt[n]):
            # If it is true then break the inner loop using the break keyword.
            break

            # After the end of the inner For loop check if the inner loop iterator value
        # is equal to the length of the given list - 1 using the if conditional statement.
    if(n == lstleng-1):
        # If it is true then it is the leader so print it.
        print(gvnlstt[m])

Output:

The leaders of the given list [23, 11, 1, 7, 8, 6, 3] are :
23
11
8
6
3

Method #2: Using For loop (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 list and store it in another variable.
  • Loop from 0 to the length of the list using the For loop.
  • Loop from parent loop iterator value to the length of the list using another Nested for loop(Inner For loop)
  • Check if the element at the index of the parent loop iterator value is less than or equal to the element at the index of the inner loop iterator value.(gvnlist [ m ] <= gvnlist [ n ] ) using the if conditional statement.
  • If it is true then break the inner loop using the break keyword.
  • After the end of the inner For loop check if the inner loop iterator value is equal to the length of the given list -1 using the if conditional statement.
  • If it is true then it is the leader so print it.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlstt = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the list and store it in another variable.
lstleng = len(gvnlstt)
print('The leaders of the given list', gvnlstt, 'are :')
# Loop from 0 to the length of the list using the For loop.
for m in range(lstleng):
    # Loop from parent loop iterator value to the length of the list
    # using another Nested for loop(Inner For loop)
    for n in range(m+1, lstleng):
            # Check if the element at the index of the parent loop iterator value
        # is less than or equal to the element at the index of the inner loop iterator value.
        # (gvnlist[m] <= gvnlist[n]) using the if conditional statement.
        if (gvnlstt[m] <= gvnlstt[n]):
            # If it is true then break the inner loop using the break keyword.
            break

            # After the end of the inner For loop check if the inner loop iterator value
        # is equal to the length of the given list - 1 using the if conditional statement.
    if(n == lstleng-1):
        # If it is true then it is the leader so print it.
        print(gvnlstt[m])

Output:

Enter some random List Elements separated by spaces = 1 2 3 7 8 6
The leaders of the given list [1, 2, 3, 7, 8, 6] are :
8
6

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.