Answer to Question #207390 in Python for rakesh

Question #207390

Leaders

You are given a list(

A) of integers. An element is a leader if it is greater than all the elements to its right side. And the rightmost element is always a leader.Write a program to print all the leaders in the list by maintaining their relative order.

Input

The input contains space-separated integers denoting the elements of the list

A.Output

The output should have the numbers in

A that are leaders.Explanation

Given

A = [4, 5, 1, 3, 2].The number

5 has no numbers that are greater than itself to its right side. The number 3 has no numbers that are greater than itself to its right side. The last number 2 is always a leader.So the leaders are

5 3 2.

Sample Input 1

4 5 1 3 2

Sample Output 1

5 3 2

Sample Input 2

9 8 10 12

Sample Output 2

12


1
Expert's answer
2021-06-16T01:00:52-0400
def main():
    a = list(map(int, input().split()))
    r = [a[-1]]
    for i in reversed(a):
        if i > r[-1]:
            r.append(i)
    for i in reversed(r):
        print(i, end=' ')


if __name__ == '__main__':
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS