Company: KLA
Difficulty: medium
Kth Next greater element for each index in an array You are given an array arr of n integers (indexed from 0 ) and an integer k . For every index i , look only at the positions j > i whose value is strictly greater than arr[i] . Sort those candidates by value in increasing order, breaking ties between equal values by the smaller index. The answer for i is the index j of the k -th candidate in that order. If index i has fewer than k such candidates, its answer is -1 . Print the answer for every index. Input Format The first line contains two integers n and k . The second line contains n space-separated integers arr[0], arr[1], ..., arr[n-1] . Output Format Print n space-separated integers on one line: the answer for index 0 , index 1 , ..., index n-1 . Constraints 1 <= n <= 2500 (inferred -- the source constraints are blurred; the bound keeps the full answer line under the judge's output limit) 1 <= k <= n (inferred) -10^9 <= arr[i] <= 10^9 (inferred) Ties between e