Find every element greater than all elements to its right

Quick 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.

Find every element greater than all elements to its right

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `nums`, find all leaders in the array. An element is a leader if it is strictly greater than all elements to its right. The rightmost element is always a leader. Return the leaders in the order they appear in the array. ### Function Signature ```python def find_leaders(nums: list[int]) -> list[int]: ... ``` ### Rules - The comparison is strict: an element that equals any value to its right is not a leader. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^4 <= nums[i] <= 10^4` ### Examples ```text Input: nums = [16,17,4,3,5,2] Output: [17,5,2] ``` 17 is greater than 4, 3, 5 and 2, 5 is greater than 2, and 2 is the rightmost element. 16, 4 and 3 each have a larger value somewhere to their right. ```text Input: nums = [1,2,3,4,0] Output: [4,0] ``` 4 is greater than the 0 after it, and 0 is the rightmost element. Each of 1, 2 and 3 has a larger value to its right. ```text Input: nums = [5,3,5,1] Output: [5,1] ``` The 5 in the output is the one at index 2. The 5 at index 0 is not a leader because an equal 5 sits to its right, and 3 is smaller than that 5.

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.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, find all leaders in the array. An element is a leader if it is strictly greater than all elements to its right. The rightmost element is always a leader.

Return the leaders in the order they appear in the array.

Function Signature

def find_leaders(nums: list[int]) -> list[int]:
    ...

Rules

  • The comparison is strict: an element that equals any value to its right is not a leader.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^4 <= nums[i] <= 10^4

Examples

Input: nums = [16,17,4,3,5,2]
Output: [17,5,2]

17 is greater than 4, 3, 5 and 2, 5 is greater than 2, and 2 is the rightmost element. 16, 4 and 3 each have a larger value somewhere to their right.

Input: nums = [1,2,3,4,0]
Output: [4,0]

4 is greater than the 0 after it, and 0 is the rightmost element. Each of 1, 2 and 3 has a larger value to its right.

Input: nums = [5,3,5,1]
Output: [5,1]

The 5 in the output is the one at index 2. The 5 at index 0 is not a leader because an equal 5 sits to its right, and 3 is smaller than that 5.