Array Leaders

Easy Arrays & Hashing Right to Left Traversal Original on GeeksforGeeks

Array Leaders is a easy arrays & hashing problem solved with the right to left traversal pattern. The best approach, optimal (right-to-left max), runs in O(n) time and O(1) space. Below are 2 approaches in Java, from brute force up.

Problem

An element is a leader if it is greater than or equal to every element to its right. The rightmost element is always a leader. Return all leaders in the order they appear in the array.

Examples

Example 1

Input
arr = [10, 4, 2, 4, 1]
Output
[10, 4, 4, 1]
Why
10 beats everything after it; the second 4 ties nothing larger after it; 1 is last.

Example 2

Input
arr = [5, 7, 3]
Output
[7, 3]

Constraints

  • 1 <= arr.length <= 10^6
  • 0 <= arr[i] <= 10^6
  • An element is a leader if it is greater than or equal to every element to its right.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.

ApproachTimeSpace
Brute forceO(n²)O(1)
Optimal (right-to-left max)O(n)O(1)

1Brute force

TimeO(n²)
SpaceO(1)Besides the output list.

For each element, scan everything to its right and check that nothing is larger.

  1. For each i, check arr[j] <= arr[i] for all j > i.
  2. Collect i if the check passes.
Java
class Solution {
    static ArrayList<Integer> leaders(int[] arr) {
        ArrayList<Integer> out = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            boolean leader = true;
            for (int j = i + 1; j < arr.length; j++)
                if (arr[j] > arr[i]) { leader = false; break; }
            if (leader) out.add(arr[i]);
        }
        return out;
    }
}

2Optimal (right-to-left max)

TimeO(n)
SpaceO(1)Besides the output list.

Traverse from the right with the maximum seen so far. An element is a leader when it is at least that maximum. Collect leaders in reverse, then reverse the list to restore the original order.

  1. max = -infinity.
  2. For i from n - 1 to 0: if arr[i] >= max, add it and set max = arr[i].
  3. Reverse the collected list.
Java
class Solution {
    static ArrayList<Integer> leaders(int[] arr) {
        ArrayList<Integer> out = new ArrayList<>();
        int max = Integer.MIN_VALUE;
        for (int i = arr.length - 1; i >= 0; i--) {
            if (arr[i] >= max) {
                out.add(arr[i]);
                max = arr[i];
            }
        }
        Collections.reverse(out);
        return out;
    }
}

Edge cases to test

  • The last element is always a leader
  • Equal values (>= counts as a leader)
  • A strictly increasing array has only one leader

Hints

Hint 1

Walk from the right while tracking the maximum seen so far.

FAQ

What is the best time complexity for Array Leaders?

Optimal (right-to-left max) runs in O(n) time and O(1) extra space.

Which pattern does Array Leaders use?

It is a arrays & hashing problem that uses the right to left traversal pattern. Other problems with the same pattern: Product of Array Except Self.

Is there a brute force solution for Array Leaders?

Yes. Brute force takes O(n²) time and O(1) space. For each element, scan everything to its right and check that nothing is larger.

Which edge cases should I test for Array Leaders?

The last element is always a leader; Equal values (= counts as a leader); A strictly increasing array has only one leader.