Practice · 2 of 5
Closest Value
CHALLENGE
Difficulty: intermediate+25 XP
**Description:** Given a sorted array of n integers and q queries, for
each query x print the array value closest to x. If two values are
equally close, print the **smaller** one.
**Input:** Line 1: n q (1 <= n, q <= 10^5). Line 2: n distinct sorted
integers (|a_i| <= 10^9). Next q lines: x (|x| <= 10^9).
**Output:** q lines — the closest value.
**Example:** 4 2 / 1 4 8 12 / 5 / 10 -> 4, 8
(5 is 1 away from 4 and 3 from 8 -> 4; 10 ties 8 and 12 -> smaller, 8).
Press Submit to check your solution.
Back to lesson: Practice: Search Set