Given an array A of positive integers. Your task is to find the leaders in the array. An element of array is leader if it is greater than or equal to all the elements to its right side. The rightmost element is always a leader. 
Example 1:
Input:
n = 6
A[] = {16,17,4,3,5,2}
Output: 17 5 2
Explanation: The first leader is 17 
as it is greater than all the elements
to its right.  Similarly, the next 
leader is 5. The right most element 
is always a leader so it is also 
included.
 
Example 2:
Input:
n = 5
A[] = {1,2,3,4,0}
Output: 4 0
Explanation: The first leader is 4
as all the other numbers aren't greater than
the other elements to their right side.
The second leader is 0 as there are no elements
at right side so its greater itself.
 
Your Task:
You don't need to read input or print anything. The task is to complete the function leader() which takes array A and n as input parameters and returns an array of leaders in order of their appearance.
 
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)
Constraints:
1 <= n <= 10^{5}
0 <= A_{i} <= 10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: class Solution:

    #Back-end complete function Template for Python 3

    

    #Function to find the leaders in the array.

    def leaders(self, A, N):

        #Code here

Solutions:
class Solution:

	def leaders(self, A, N):
		a = []
		m = A[-1]
		for i in range(N - 1, -1, -1):
			if A[i] >= m:
				m = A[i]
				a.append(A[i])
		return a[::-1]


class Solution:

	def leaders(self, A, N):
		l = [A[-1]]
		max_so_far = A[-1]
		for i in range(-2, -(N + 1), -1):
			if A[i] >= max_so_far:
				l.append(A[i])
				max_so_far = A[i]
		return reversed(l)


class Solution:

	def leaders(self, A, N):
		n = 0
		l = []
		for i in range(len(A) - 1, -1, -1):
			if A[i] >= n:
				n = A[i]
				l.append(n)
		l = l[::-1]
		return l


class Solution:

	def leaders(self, A, N):
		mx = -99999999
		lst = []
		A.reverse()
		lst.append(A[0])
		for i in range(0, N - 1):
			mx = max(A[i], lst[-1])
			if A[i + 1] >= mx:
				lst.append(A[i + 1])
		return lst[::-1]


class Solution:

	def leaders(self, A, N):
		ans = []
		stack = [N - 1]
		right = [-1] * N
		for i in range(N - 2, -1, -1):
			while stack and A[stack[-1]] <= A[i]:
				stack.pop()
			if stack:
				right[i] = stack[-1]
			stack.append(i)
		for i in range(N):
			if right[i] == -1:
				ans.append(A[i])
		return ans


class Solution:

	def leaders(self, A, N):
		res = [A[-1]]
		m = A[-1]
		for i in range(N - 2, -1, -1):
			if A[i] >= m:
				res.append(A[i])
				m = A[i]
		return res[::-1]


class Solution:

	def leaders(self, a, n):
		aux = [0] * n
		result = []
		aux[n - 1] = a[-1]
		for i in range(n - 2, -1, -1):
			aux[i] = max(a[i], aux[i + 1])
		for i in range(n):
			if a[i] >= aux[i]:
				result.append(a[i])
		return result


class Solution:

	def leaders(self, A, N):
		A.reverse()
		stack = [A[0]]
		for i in range(1, N):
			if A[i] >= stack[-1]:
				stack.append(A[i])
		stack.reverse()
		return stack


class Solution:

	def leaders(self, A, N):
		right = [0] * N
		right[-1] = A[-1]
		ans = [A[-1]]
		for i in range(N - 2, -1, -1):
			right[i] = max(right[i + 1], A[i])
			if right[i] == A[i]:
				ans.append(A[i])
		ans = ans[::-1]
		return ans


class Solution:

	def leaders(self, a, n):
		l = [a[-1]]
		mx = a[-1]
		for i in range(1, n):
			if a[-1 - i] >= mx:
				l.append(a[-1 - i])
				mx = a[-1 - i]
		l = l[::-1]
		return l


class Solution:

	def leaders(self, A, N):
		Max = 0
		O = []
		for i in range(1, N + 1):
			i = -i
			if A[i] >= Max:
				Max = A[i]
				O.append(A[i])
		return O[::-1]


class Solution:

	def leaders(self, a, n):
		m = a[n - 1]
		l = [a[n - 1]]
		for i in range(n - 2, -1, -1):
			if a[i] >= m:
				m = a[i]
				l.append(m)
		l.reverse()
		return l


class Solution:

	def leaders(self, A, N):
		l = [A[N - 1]]
		max = A[N - 1]
		for i in range(N - 2, -1, -1):
			if A[i] >= max:
				max = A[i]
				l.append(A[i])
		l.reverse()
		return l


class Solution:

	def leaders(self, A, N):
		B = [A[-1]]
		for i in range(len(A) - 2, -1, -1):
			if A[i] >= B[-1]:
				B.append(A[i])
		return B[::-1]


class Solution:

	def leaders(self, A, N):
		s = A[N - 1]
		a = [s]
		for i in range(N - 2, -1, -1):
			if A[i] >= s:
				a.append(A[i])
				s = A[i]
		a.reverse()
		return a


class Solution:

	def leaders(self, A, N):
		max = A[-1]
		B = []
		for i in range(N - 1, -1, -1):
			if A[i] >= max:
				max = A[i]
				B.insert(0, max)
		return B


class Solution:

	def leaders(self, A, N):
		ans = []
		leader = -1
		for a in reversed(A):
			if a >= leader:
				ans.append(a)
				leader = a
		ans.reverse()
		return ans