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.

Given an integer array `nums`, return every leader in the array. An element is a leader when it is strictly greater than all elements to its right. The rightmost element has no elements to its right, so it is always a leader. The comparison is strict: an element that equals any value to its right is not a leader. Return the leaders as a list in the order they appear in `nums`, from left to right. Implement `find_leaders(nums)`. Example 1: ```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. Example 2: ```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. Constraints: - 1 <= len(nums) <= 10^5 - -10^4 <= nums[i] <= 10^4 - Every input value and every output element fits in a signed 32-bit integer, so int is wide enough in Java and C++.

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

  1. The rightmost element has nothing to its right, so it always qualifies.
  2. An element that equals any value to its right does not qualify, so check how your comparison treats ties.
  3. Return the leaders in the same left-to-right order they have in nums, whatever order you find them in.

Leaders in an Integer Array

easyGeneralSoftware Engineer

Given an integer array nums, return every leader in the array. An element is a leader when it is strictly greater than all elements to its right. The rightmost element has no elements to its right, so it is always a leader.

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

Return the leaders as a list in the order they appear in nums, from left to right.

Implement find_leaders(nums).

Example 1:

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.

Example 2:

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.

Constraints:

  • 1 <= len(nums) <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • Every input value and every output element fits in a signed 32-bit integer, so int is wide enough in Java and C++.

Examples

Example 1
Input
([16, 17, 4, 3, 5, 2],)
Output
[17, 5, 2]
Notes
Source example 1: 17, 5 and 2 are leaders, while 16, 4 and 3 each have a larger value to their right.
Example 2
Input
([1, 2, 3, 4, 0],)
Output
[4, 0]
Notes
Source example 2: only 4 and the trailing 0 qualify after an increasing run.

Constraints

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

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def find_leaders(nums: list[int]) -> list[int]:
Sample call
find_leaders([16, 17, 4, 3, 5, 2])
Language
Case 1
Input
([16, 17, 4, 3, 5, 2],)
Expected
[17, 5, 2]
Case 2
Input
([1, 2, 3, 4, 0],)
Expected
[4, 0]

Browse More Questions

Show the approach

Approach

Scan nums from right to left while tracking the largest value seen so far, which is the maximum of everything to the right of the current position. The rightmost element is a leader by definition and seeds that running maximum. Every other element is a leader exactly when it is strictly greater than the running maximum, because being greater than every element to the right is the same as being greater than the largest of them. When an element qualifies, record it and make it the new running maximum. Leaders are found from right to left, so the recorded list is reversed before it is returned to restore left-to-right order.

Invariant: just before index i is examined, the running maximum equals max(nums[i+1:]), and the recorded list holds exactly the leaders at indices greater than i, rightmost first.

Edge cases: a single element is returned on its own. When all values are equal only the last copy qualifies, since each earlier copy ties with a value to its right. An all-negative array is handled because the running maximum is seeded with the rightmost element, so no placeholder such as 0 can hide a negative leader. The returned list is always strictly decreasing.

Time complexity:
O(n)
Space complexity:
O(n)