A radio scanner knows n station frequencies (integers, possibly repeated). Given a target dial value x and an integer k, choose the k frequencies that are closest to x, where closeness is measured by absolute difference |f - x|. When two frequencies are equally close, prefer the one with the smaller value. After choosing the k frequencies, output them sorted in ascending order.
Line 1: three integers n, k, and x.
Line 2: n space-separated integers, the frequencies.
k space-separated integers on one line: the chosen frequencies sorted ascending.
Example 1
Input
5 3 100 80 95 102 130 100
Expected
95 100 102
Explanation
Distances from 100: 80->20, 95->5, 102->2, 130->30, 100->0. The three closest are 100, 102, 95; sorted ascending they are 95 100 102.
Example 2
Input
5 2 6 4 8 6 3 9
Expected
4 6
Explanation
Distances from 6: 4->2, 8->2, 6->0, 3->3, 9->3. Closest is 6 (0). The next tie is 4 and 8, both distance 2; the smaller value 4 wins. Sorted ascending: 4 6.
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 →