Kth Largest Element: Return Its Original Index
Problem statement
Given an integer array nums and a one-based rank k, return the original zero-based index of its kth largest occurrence. Duplicate occurrences occupy separate ranks.
Rank entries by larger value first. When values are equal, rank the entry with the smaller original index first. For example, if nums = [5, 5, 5], the indices in rank order are [0, 1, 2]. Return the index at rank k. Do not change nums.
Function
kthLargestIndex(nums: int[], k: int) → intExamples
Example 1
nums = [3,2,1,5,6,4]k = 2return = 3The descending values are 6 at index 4 and 5 at index 3, so rank two returns index 3.
Example 2
nums = [5,5,5,2]k = 2return = 1Equal fives occupy the first three ranks in original-index order 0, 1, 2.
Example 3
nums = [3,2,3,1,2,4,5,5,6]k = 4return = 5Ranks one through four have values 6, 5, 5, and 4; the value 4 was at original index 5.
Constraints
1 <= k <= nums.length <= 100000.-10000 <= nums[i] <= 10000.