-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeaders in an array.py
30 lines (25 loc) · 1.09 KB
/
Leaders in an array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
https://practice.geeksforgeeks.org/problems/leaders-in-an-array/0
Given an array of positive integers. Your task is to find the leaders in the array.
Note: An element of array is leader if it is greater than or equal to all the elements to its right side. Also, the rightmost element is always a leader.
"""
__author__ = 'abhireddy96'
from typing import List
class Solution:
def findLeaders(self, nums: List[int]) -> List[int]:
res = list()
# Consider last element as Pivot, add it to resultant array as last or rightmost element is always Leader
pivot = nums[-1]
res.append(pivot)
# Iterate from penultimate to starting index
for i in range(len(nums)-2, -1, -1):
# Element should be greater that pivot, in order to be Leader
if nums[i] >= pivot:
pivot = nums[i]
res.append(pivot)
# Reverse resultant array
return list(reversed(res))
if __name__ == "__main__":
for _ in range(int(input())):
n = input()
print(Solution().findLeaders(list(map(int, input().split()))))