You are given an array of N positive integers, A1,A2,…,An. You have to answer Q queries. Each query consists of two integers L and K. For each query I have to tell the Kth element which is larger than or equal to L in the array when all such elements are listed according to increasing order of their indices.
Example A = 22,44,12,16,14,88,25,49
Query 1: L = 3 K = 4 Since all elements are greater than 3. So we list the whole array i.e. 22,44,12,16,14,88,25,49. 4th element among these elements is 16
Query 2: L = 19 K = 5 Listed elements 22,44,88,25,49. 5th element among these is 49.
What I have done: for each query iterate over the whole array and check the Kth element which is larger than or equal to L. Complexity : O(Q*N)
What I require: O(Q*logN) complexity.
Constraints : 1<=Q<=10^5 1<=N<=10^5 1<=Ai<=10^5