Find every element greater than all elements to its right
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
Overview: Find every element of an integer array that is strictly greater than all elements to its right, and return them in their original order. It tests linear-time array scanning and careful handling of ties and negative values.
Constraints
- 1 <= len(nums) <= 10^5
- -10^4 <= nums[i] <= 10^4
Examples
Input: ([16, 17, 4, 3, 5, 2],)
Expected Output: [17, 5, 2]
Explanation: Source example 1: 17, 5 and 2 are leaders, while 16, 4 and 3 each have a larger value to their right.
Input: ([1, 2, 3, 4, 0],)
Expected Output: [4, 0]
Explanation: Source example 2: only 4 and the trailing 0 qualify after an increasing run.
Hints
- The rightmost element has nothing to its right, so it always qualifies.
- An element that equals any value to its right does not qualify, so check how your comparison treats ties.
- Return the leaders in the same left-to-right order they have in nums, whatever order you find them in.