Problem · Array
Array Leader Elements
Learn this problemProblem statement
Given an integer array nums, return every leader element in its original left-to-right order.
An element nums[i] is a leader when it is strictly greater than every element to its right. The final element is always a leader because no elements follow it.
If equal values occur, an earlier value is not a leader when the same value appears to its right.
Function
findLeaders(nums: int[]) → int[]Examples
Example 1
nums = [16,17,4,3,5,2]return = [17,5,2]17 is greater than every value after it. The same is true for 5 and the final value 2. Each other value has a larger value somewhere to its right.
Example 2
nums = [9,7,5,3]return = [9,7,5,3]The array is strictly decreasing, so every value is greater than all values to its right.
Example 3
nums = [7,7,3]return = [7,3]The first 7 is not strictly greater than the equal 7 to its right. The second 7 and the final value 3 are leaders.
Constraints
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9