#C1976. Minimize Travel Distance
Minimize Travel Distance
Minimize Travel Distance
You are given a series of delivery requests. Each request is associated with a building number where a delivery needs to be made. In order to minimize the total travel distance for all deliveries, you must choose a building as the central delivery point. The optimal building is the median of the given building numbers. In other words, if the building numbers are sorted, the answer is the element at index \(rac{N-1}{2}\) (using 0-indexing). Note that when there is an even number of building requests, we choose the lower median.
Example: For the input list [1, 2, 9, 12, 25], after sorting the list remains [1, 2, 9, 12, 25] and the median (3rd element) is 9, which is the answer.
inputFormat
The first line of input contains a single integer \(N\) which represents the number of delivery requests. The second line contains \(N\) space-separated integers representing the building numbers for each delivery request.
Input Format:
N b1 b2 ... bNwhere \(b_i\) is the building number for the \(i^{th}\) delivery.
outputFormat
Output a single integer, the building number that minimizes the total travel distance, i.e. the median as described above.
## sample5
1 2 9 12 25
9