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.

Answer these:

  1. Write a program to print all the leaders in the array in python?
  2. Leaders in array coding ninjas?
  3. Write a program to print all the leaders in the array in c?
  4. Leaders in an array java program?
  5. Leaders in array coding ninjas github?
  6. Sum of leaders in an array?
  7. Leaders in an array gfg practice?
  8. Leaders in an array leetcode?
  9. Leaders in array coding ninjas?
  10. Leaders in an array java program?
  11. Leaders in array coding ninjas github?
  12. Lum of leaders in an array?
  13. Leaders in an array gfg practice?
  14. Leaders in an array leetcode?
  15. Find leaders in array?
  16. How to find leaders in an array?
  17. Find leaders in an array leetcode?
  18. Python program to find an element in a list?
  19. Python program to find adjacent elements in a list?

Related Posts On