Kth Greater Element Index to the Right
Problem statement
For each array position, scan strictly to its right and consider positions whose value is strictly greater than the current value.
Return the 1-based index of the kth such position in left-to-right encounter order, or -1 if fewer than k exist. Equal values do not qualify.
Function
kthGreaterIndexes(nums: int[], k: int) → int[]Examples
Example 1
nums = [2,4,0,9,6]k = 2return = [4,5,5,-1,-1]For value 2, the second greater position is 4; other positions are evaluated independently.
Example 2
nums = [3,1,5,2,4]k = 1return = [3,3,-1,5,-1]Return each next encountered greater position.
Example 3
nums = []k = 1return = []Empty input gives an empty result.
Constraints
0 <= nums.length <= 5000.1 <= k <= nums.lengthwhen the array is nonempty.