A community radio cooperative has already assigned n distinct broadcast frequencies to its transmitter towers. Whenever a listener requests a frequency to tune to, the cooperative reports two bracket frequencies chosen from among the ones already assigned: the largest assigned frequency that does not exceed the request, and the smallest assigned frequency that is not smaller than the request. Either bracket may fail to exist, in which case it is reported as -1.
Given the n assigned frequencies and q listener requests, compute both brackets for every request.
n.n distinct integers, the assigned frequencies (given in no particular order).q.q integers, the requested frequencies, in the order they should be answered.Print q lines. On the i-th line print two integers separated by a single space: the floor bracket (the largest assigned frequency <= the i-th request, or -1 if none) followed by the ceiling bracket (the smallest assigned frequency >= the i-th request, or -1 if none).
1 <= n <= 2000001 <= q <= 2000000 <= frequency <= 10^9 for both assigned frequencies and requests.n assigned frequencies are pairwise distinct.Example 1
Input
5 90 101 200 350 500 3 100 200 501
Expected
90 101 200 200 500 -1
Explanation
Assigned frequencies sorted are 90, 101, 200, 350, 500. For request 100: the largest assigned value <=100 is 90 (floor), and the smallest assigned value >=100 is 101 (ceiling) -> "90 101". For request 200: 200 itself is assigned, so floor=ceiling=200 -> "200 200". For request 501: floor is 500 (largest <=501), and no assigned frequency is >=501, so ceiling=-1 -> "500 -1".
Example 2
Input
1 500 3 500 100 999
Expected
500 500 -1 500 500 -1
Explanation
Only frequency 500 is assigned. Request 500 matches exactly, giving "500 500". Request 100 is below the only frequency, so floor=-1 and ceiling=500, giving "-1 500". Request 999 is above it, so floor=500 and ceiling=-1, giving "500 -1".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →