#K45937. Minimum Delivery Distance
Minimum Delivery Distance
Minimum Delivery Distance
You are given a list of integer coordinates representing the locations of houses along a straight street. A postman needs to deliver mail to all houses. The postman can start at any house, but once he starts his delivery, he must travel along the street. Your task is to determine the minimum distance the postman needs to travel in order to deliver mail to all houses.
The optimal strategy is to start at the house with the smallest coordinate and finish at the house with the largest coordinate (or vice versa). Mathematically, if the house coordinates are \(x_1, x_2, \ldots, x_n\), then the minimum required traveling distance is given by:
[ \text{distance} = \max_{1 \leq i \leq n} (x_i) - \min_{1 \leq i \leq n} (x_i) ]
Note that if there is only one house, the postman does not need to travel at all.
inputFormat
The first line of input contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) indicating the number of houses. The second line contains \(n\) space-separated integers, each representing the coordinate of a house. Each coordinate \(x_i\) satisfies \(|x_i| \leq 10^9\).
outputFormat
Output a single integer representing the minimum distance the postman must travel to deliver mail to all houses.
## sample4
2 5 7 12
10