Problem

Kth Greater Element Indexes

UberFULLTIMEOA
See Uber hiring insights

You are given an integer array nums and an integer k. For each index i, look at the elements to the right of i whose value is strictly greater than nums[i], preserving their original left-to-right order.

Return an integer array answer where answer[i] is the 1-indexed position of the k-th such greater element for index i. If fewer than k greater elements appear to the right of i, set answer[i] to -1.

Examples
01 · Example 1
nums = [3, 4, 2, 6, 5]
k = 2
return = [4, 5, 5, -1, -1]

For 3, the greater elements to the right are 4, 6, 5, so the 2nd one is 6 at 1-indexed position 4. For 4, the greater elements are 6, 5, so the answer is position 5.

02 · Example 2
nums = [5, 1, 4, 2, 3]
k = 1
return = [-1, 3, -1, 5, -1]

For 1, the first greater element to its right is 4 at position 3. For 2, it is 3 at position 5.

Constraints
  • 1 <= nums.length <= 2 * 10^5
  • 1 <= k <= nums.length
  • -10^9 <= nums[i] <= 10^9
More Uber problems
drafts saved locally
public int[] kthGreaterElementIndexes(int[] nums, int k) {
  // write your code here
}
nums[3, 4, 2, 6, 5]
k2
expected[4, 5, 5, -1, -1]
sign in to submit