Problems > K Nearest Values

K Nearest Values

Difficulty: 1400 3500 ms 128 MB

sorting two-pointers

Print the K values closest to X, in ascending order. Break distance ties by preferring the SMALLER value.

Input: the first line has N, K and X; the second line has N integers.

Output: the K chosen values, ascending.

Constraints:

  • 1 ≤ K ≤ N ≤ 105
  • |values|, |X| ≤ 106

Sample Tests

Input 1
5 3 4
1 2 3 7 9
Output 1
1 2 3
Input 2
2 1 5
4 6
Output 2
4

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.