Distances

View as PDF

Submit solution

Points: 5
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type

Given an array of length N and Q queries, print the closest number to the value given in each query.

Input Specification

The first line will contain integer N (1 \leq N \leq 10^5) and Q (1 \leq Q \leq 10^5), the length of the array and the number of queries, respectively. The next line will contain N integers, the values in the array. It is guaranteed that these values will be smaller than 10^9. The next Q lines will contain an integer a (1 \leq a \leq 10^9), the value that you are to find the closest number to.

Output Specification

For each query, one integer that is either the integer in the array that is closest to a. If there are multiple numbers that have the same distance from a print the largest one.

Subtasks

Subtask 1 (50%)

N,Q \leq 10^3

Subtask 2 (50%)

No further constraints.

Sample Input 1

5 5
2 3 6 9 10
1
2
6
13
9

Sample Output 1

2
2
6
10
9

Comments

There are no comments at the moment.