Java Array Program · A23
Find Leaders in an Array in Java
An element is a leader if it is larger than everything after it. Scan from the right and keep the largest value seen so far.
Problem Statement
Return every leader of an int array, in left-to-right order. A leader is strictly greater than every element to its right.
The last element is always a leader, because nothing comes after it.
Input/Output Examples
[12, 30, 7, 18, 4, 9]Output[30, 18, 9]Explanation: 30 beats 7, 18, 4 and 9; 18 beats 4 and 9; 9 is last.[5, 5]Output[5]Explanation: the first 5 is not strictly greater than the second.[3]Output[3]Explanation: the last element is a leader.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.
Approach 1: Right-to-Left Scan (Recommended)
Idea: walk from the end, tracking the maximum to the right; an element larger than it is a leader.
- Start from the last element, which is always a leader.
- Add an element if it is greater than
maxRight. - Update
maxRight, then reverse the list to restore left-to-right order.
Time: O(n) · Space: O(1). Excluding the output list.
Approach 2: Compare With Every Element to the Right
Idea: for each element, check all elements after it; it is a leader if none is greater or equal.
- For each index
i, loop overj > i. - Stop if
arr[j] ≥ arr[i]. - Add
arr[i]if no suchjexists.
Time: O(n²) · Space: O(1).
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.
Approach 1: Right-to-Left Scan (Recommended)
The final Collections.reverse() restores the order in which the leaders appear in the array.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class LeadersRightScan {
// Scanning from the right, an element is a leader if it beats the maximum seen so far
static List<Integer> leaders(int[] arr) {
List<Integer> result = new ArrayList<>();
int maxRight = Integer.MIN_VALUE;
for (int i = arr.length - 1; i >= 0; i--) {
if (i == arr.length - 1 || arr[i] > maxRight) {
result.add(arr[i]);
}
maxRight = Math.max(maxRight, arr[i]);
}
Collections.reverse(result); // back to left-to-right order
return result;
}
public static void main(String[] args) {
int[][] inputs = {{12, 30, 7, 18, 4, 9}, {5, 5}, {3}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + leaders(arr));
}
}
}
[12, 30, 7, 18, 4, 9] -> [30, 18, 9] [5, 5] -> [5] [3] -> [3]
Time complexity: O(n) · Space complexity: O(1).
Approach 2: Compare With Every Element to the Right
A direct translation of the definition, useful for checking the fast version.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LeadersBrute {
// Compares each element with every element to its right: O(n^2)
static List<Integer> leaders(int[] arr) {
List<Integer> result = 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) {
result.add(arr[i]);
}
}
return result;
}
public static void main(String[] args) {
int[][] inputs = {{12, 30, 7, 18, 4, 9}, {5, 5}, {3}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + leaders(arr));
}
}
}
[12, 30, 7, 18, 4, 9] -> [30, 18, 9] [5, 5] -> [5] [3] -> [3]
Time complexity: O(n²) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Right-to-Left Scan | O(n) | O(1) |
| Compare With Every Element to the Right | O(n²) | O(1) |
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.