GeeksforGeeks Easy

Leaders in an Array

Leaders in an Array is a GFG problem (Easy).

You are given an array arr of non-negative integers. An element is a leader if it is greater than or equal to every element to its right. Return all the leaders in the order they appear in arr. GFG lists the problem as Array Leaders.

  • The rightmost element is always a leader, since nothing is to its right.
  • Ties count: an element equal to the largest value on its right is still a leader.

The array can hold 10⁶ elements, so comparing every element with everything after it is too slow. The target is one pass.

Constraints
  • 1 <= arr.size() <= 10⁶
  • 0 <= arr[i] <= 10⁶
arraysuffix
Open on GeeksforGeeks ↗
02

Intuition

An element is a leader when nothing to its right is bigger. Checking that directly means re-reading everything to the right of every element.

The trick is to walk the array from the right end. Then "everything to the right" is exactly the part you have already passed, and you need only one fact about it: its largest value. Keep that in one variable, max_right.

An element is a leader exactly when it is at least max_right, because beating the largest value means beating all of them. Each element is looked at once, so the scan is O(n).

How to spot this pattern

When each element's answer depends on everything to one side of it, scan from that side and carry a running summary; here the summary is a single number, the largest value seen. LeetCode has no problem by this name, so the closest match is LeetCode 1299 (Replace Elements with Greatest Element on Right Side), which uses the same backward scan.

03

Approach

Try it first

Before reading on: for [16, 17, 4, 3, 5, 2], walk from the right and write down the largest value seen so far at each step. Which elements are at least as big as that value?

1

Start with nothing seen

Set res = [] and max_right to minus infinity. Nothing has been seen yet, so the last element always passes the test, which matches the rule that the rightmost element is a leader.

2

Scan from right to left

Visit the elements from the last one back to the first. For each element x:

  • x >= max_right: nothing to its right is bigger, so x is a leader. Append it and set max_right = x.
  • otherwise: a bigger element sits to its right, so skip it.

Use >=, not >, so equal values are kept.

3

Reverse the result

The leaders were found from right to left, so res holds them backwards. Reverse it once at the end to return them in their original order.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def leaders(self, arr):
▶3 res = []
▶4 max_right = float("-inf")
▶5 for x in reversed(arr):
▶6 if x >= max_right:
▶7 res.append(x)
▶8 max_right = x
▶9 res.reverse()
▶10 return res
arr16017142335425check−∞max rightleadersemptystart at the right end, max right = −∞
max_right−∞nothing seen yet
resempty
Start. The scan walks from the last element back to the first. max_right will hold the largest value already passed, which is the largest value to the right of the current element. It starts at minus infinity, so the last element is sure to pass.
arr16017142335425check2element≥−∞max rightleaders22 ≥ −∞ → leader, max right = 2
x2index 5
max_right2
res2
2 is the last element: nothing is to its right, so it is a leader. It becomes max_right.
arr16017142335425check5element≥2max rightleaders255 ≥ 2 → leader, max right = 5
x5index 4
max_right5
res2, 5
5 is at least 2, the largest value to its right, so nothing on its right is bigger: it is a leader. It is now the largest value seen, so max_right = 5.
arr16017142335425check3element<5max rightleaders253 < 5 → not a leader
x3index 3
max_right5
res2, 5
3 is smaller than 5, which sits somewhere to its right, so it is not a leader. max_right stays 5.
arr16017142335425check4element<5max rightleaders254 < 5 → not a leader
x4index 2
max_right5
res2, 5
4 is smaller than 5, which sits somewhere to its right, so it is not a leader. max_right stays 5.
arr16017142335425check17element≥5max rightleaders251717 ≥ 5 → leader, max right = 17
x17index 1
max_right17
res2, 5, 17
17 is at least 5, the largest value to its right, so nothing on its right is bigger: it is a leader. It is now the largest value seen, so max_right = 17.
arr16017142335425check16element<17max rightleaders251716 < 17 → not a leader
x16index 0
max_right17
res2, 5, 17
16 is smaller than 17, which sits somewhere to its right, so it is not a leader. max_right stays 17.
arr16017142335425check17max rightleaders1752found right to left → reverse
res17, 5, 2
The leaders were found from right to left, so the list was backwards. One reverse puts them back in the order they appear in the array.
arr16017142335425check17max rightleaders1752return [17, 5, 2]
answer[17, 5, 2]
Answer [17, 5, 2]. Each element was compared once with a single number, so the scan is O(n) time and O(1) extra space.
05

Common pitfalls

Using > instead of >=

✗ Wrong
if x > max_right:
✓ Right
if x >= max_right:

GFG counts an element equal to the largest value on its right as a leader. With >, [10, 4, 2, 4, 1] returns [10, 4, 1] and loses the first 4. Some sites use the strict rule instead, so check which one your problem states.

Returning the leaders backwards

✗ Wrong
return res
✓ Right
res.reverse()
return res

The scan finds the rightmost leader first, so res comes out as [2, 5, 17] for [16, 17, 4, 3, 5, 2]. The values are right but the order is not, and the judge compares order.

06

Complexity

Time
O(n)
Space
O(1)
One backward pass plus one reverse. The output list is not counted as extra space.
07

Leaders in an Array FAQ

Is there a brute-force way?

Yes: for every element, scan everything to its right and keep it only if nothing there is bigger. That is two nested loops, O(n²), which is far too slow for 10⁶ elements. The right-to-left scan above answers the same question with one running maximum in O(n).

Can you find them scanning left to right?

Yes, with a stack. Before pushing each x, pop every stacked value smaller than x: x sits to their right and beats them, so they are not leaders. At the end the stack holds exactly the leaders, already in order. It is also O(n), but needs O(n) extra space, so the backward scan is simpler.

How do you write it in Python?

Loop with for x in reversed(arr):, which walks backwards without index arithmetic or a copy of the list, then call res.reverse() once at the end. Avoid res.insert(0, x): each insert shifts the whole list, so the O(n) scan becomes O(n²), far too slow for 10⁶ elements.