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.
The input contains space-separated integers denoting the elements of the list
The output should have the numbers in
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
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()
Comments
Leave a comment