00:00

Problem Statement

Input/Output Examples

Visual Explanation

An element only needs to be compared with the maximum of everything to its right, and that maximum can be updated in constant time while scanning backwards.

Approaches

The right-to-left scan is linear. The brute force compares each element with every element after it.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the number of elements. The scan reads each element once; the brute force compares up to n(n - 1) / 2 pairs.

Interview Tips and Recommended Approach

  • Clarify strictness. Should equal values count? This page uses "strictly greater".
  • Scan from the right. It turns an O(n²) definition into one pass.
  • Keep the order the interviewer asks for. The scan finds leaders right to left.
  • Know the related pattern. "Next greater element" uses a stack for a similar question.

Recommended Approach: Right-to-Left Scan

Why: it finds every leader in one pass with a single running maximum.