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
 
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^{7}
0 <= A_{i} <= 10^{7}

Difficulty: EASY
Tags: Data structures
Skill types: Data structures
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)

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):
		leaders = []
		max_right = A[N - 1]
		leaders.append(max_right)
		for i in range(N - 2, -1, -1):
			if A[i] >= max_right:
				leaders.append(A[i])
				max_right = A[i]
		leaders.reverse()
		return leaders


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def leaders(self, A, N):
		temp = []
		temp.append(A[-1])
		curr_leader = A[-1]
		for i in range(N - 2, -1, -1):
			if curr_leader <= A[i]:
				curr_leader = A[i]
				temp.append(curr_leader)
		temp.sort(reverse=True)
		return temp


import collections

class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def leaders(self, A, N):
		from collections import deque
		max_ele = 0
		q = deque([])
		for i in range(N):
			j = N - i - 1
			if A[j] >= max_ele:
				max_ele = A[j]
				q.appendleft(A[j])
		return q


class Solution:

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


class Solution:

	def leaders(self, arr, n):
		lead = arr[-1]
		ans = [lead]
		i = n - 2
		while i >= 0:
			if arr[i] >= lead:
				lead = arr[i]
				ans.append(arr[i])
			i = i - 1
		return ans[::-1]


class Solution:

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


class Solution:

	def leaders(self, A, N):
		leader = A[N - 1]
		i = N - 1
		while i > 0:
			if leader <= A[i - 1]:
				leader = A[i - 1]
			else:
				A.pop(i - 1)
			i -= 1
		return A


class Solution:

	def leaders(self, A, N):
		leader = A[N - 1]
		leader_list = [leader]
		i = N - 1
		while i > 0:
			if leader <= A[i - 1]:
				leader_list.append(A[i - 1])
				leader = A[i - 1]
			i -= 1
		return leader_list[::-1]


class Solution:

	def leaders(self, arr, N):
		klead = []
		k = arr[N - 1]
		for i in range(N - 1, -1, -1):
			if arr[i] >= k:
				klead.append(arr[i])
				k = arr[i]
		klead.reverse()
		return klead


class Solution:

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


class Solution:

	def leaders(self, A, N):
		res = []
		maxi = 0
		for i in range(N - 1, -1, -1):
			maxi = max(maxi, A[i])
			if A[i] == maxi:
				res.append(A[i])
		return res[::-1]


class Solution:

	def leaders(self, A, N):
		B = []
		prevvalue = A[N - 1]
		for index in range(N - 1, -1, -1):
			if A[index] >= prevvalue:
				prevvalue = A[index]
				B.append(A[index])
		B.reverse()
		return B


class Solution:

	def leaders(self, A, N):
		b = []
		b.append(A[N - 1])
		m = A[N - 1]
		for i in range(N - 2, -1, -1):
			if A[i] >= m:
				b.append(A[i])
				m = A[i]
		start = 0
		n = len(b)
		end = n - 1
		while start < end:
			(b[start], b[end]) = (b[end], b[start])
			start += 1
			end -= 1
		return b


class Solution:

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


class Solution:

	def leaders(self, A, N):
		(res, prev) = ([], float('-inf'))
		for x in A[::-1]:
			if x >= prev:
				res.append(x)
				prev = x
		return res[::-1]


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def leaders(self, A, N):
		result = []
		max_so_far = float('-inf')
		for i in range(N - 1, -1, -1):
			if A[i] >= max_so_far:
				result.append(A[i])
				max_so_far = A[i]
		result.reverse()
		return result


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def leaders(self, A, N):
		max_ = float('-inf')
		result = []
		for index in range(N - 1, -1, -1):
			if A[index] >= max_:
				result.append(A[index])
			max_ = max(max_, A[index])
		result.reverse()
		return result


class Solution:

	def leaders(self, A, N):
		b = []
		k = 0
		for i in range(N - 1, -1, -1):
			k = max(k, A[i])
			if k == A[i]:
				b.append(A[i])
		return b[::-1]


class Solution:

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


class Solution:

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


class Solution:

	def leaders(self, arr, n):
		ans = [arr[-1]]
		for i in arr[::-1]:
			if i >= ans[-1]:
				ans.append(i)
		ans = ans[::-1]
		ans.pop()
		return ans


class Solution:

	def leaders(self, A, N):
		list = [A[N - 1]]
		i = N - 2
		sub = A[N - 1]
		while i >= 0:
			if A[i] >= sub:
				sub = A[i]
				list.append(A[i])
				i = i - 1
			else:
				i = i - 1
		list.reverse()
		return list


class Solution:

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


class Solution:

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


class Solution:

	def leaders(self, A, N):
		i = N - 2
		greatest = list()
		greatest.append(A[N - 1])
		while i > -1:
			if A[i] >= greatest[-1]:
				greatest.append(A[i])
			i -= 1
		return reversed(greatest)


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def leaders(self, A, N):
		ans = [A[-1]]
		curr = A[-1]
		for elem in A[-2::-1]:
			if elem >= curr:
				ans.append(elem)
				curr = elem
		return ans[::-1]


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):
		stack = []
		for n in A[::-1]:
			if stack == [] or stack[-1] <= n:
				stack.append(n)
		return stack[::-1]


class Solution:

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


class Solution:

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


class Solution:

	def leaders(self, A, N):
		maxi = -1
		ls = []
		for i in range(N - 1, -1, -1):
			if maxi <= A[i]:
				maxi = A[i]
				ls.append(A[i])
		return reversed(ls)


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def leaders(self, A, n):
		a = [A[n - 1]]
		max_right = A[n - 1]
		n -= 2
		while n >= 0:
			if A[n] >= max_right:
				max_right = A[n]
				a.append(A[n])
			n -= 1
		return a[::-1]


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def leaders(self, A, N):
		n = N
		nums = A
		leaders = []
		i = n - 1
		max_element = float('-inf')
		while i >= 0:
			if max_element <= nums[i]:
				max_element = max(max_element, nums[i])
				leaders.append(max_element)
			i -= 1
		return leaders[::-1]