#K88442. Closest Fountain to Midpoint
Closest Fountain to Midpoint
Closest Fountain to Midpoint
Bob loves to run along the city park, but he always gets thirsty. Along his running path, there are several fountains. Your task is to help Bob find the fountain that is closest to the midpoint of his run.
More formally, Bob starts at point ( S ) and ends at point ( E ). The midpoint is calculated as ( M = \frac{S+E}{2} ). Given a list of fountain positions, you need to select the fountain ( x ) that minimizes ( |x - M| ). In case of a tie (i.e. two fountains at equal distance from ( M )), choose the fountain with the smaller coordinate.
Example: For ( S = 0 ), ( E = 10 ) and fountains at positions [2, 5, 8], the midpoint is 5. Thus, the answer is 5.
inputFormat
The input begins with an integer ( T ) representing the number of test cases. Each test case consists of two lines:
- The first line contains three space-separated integers \( S \), \( E \), and \( F \) (where \( F \) is the number of fountains).
- The second line contains \( F \) space-separated integers representing the positions of the fountains.
outputFormat
For each test case, print the position of the fountain closest to the midpoint on a new line.## sample
3
0 10 3
2 5 8
3 17 5
1 5 10 15 20
5 25 4
5 12 18 25
5
10
12
</p>