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.
- 1 <= arr.size() <= 10⁶
- 0 <= arr[i] <= 10⁶
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).
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.
Approach
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?
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.
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, soxis a leader. Append it and setmax_right = x.- otherwise: a bigger element sits to its right, so skip it.
Use >=, not >, so equal values are kept.
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.
Code in Python, C++ and Java
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.max_right.max_right = 5.max_right stays 5.max_right stays 5.max_right = 17.max_right stays 17.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.max_right.max_right = 4.max_right stays 4.max_right stays 4.max_right = 10.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.max_right.max_right stays 40.max_right stays 40.max_right stays 40.Common pitfalls
Using > instead of >=
if x > max_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
return res
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.
Complexity
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.