#K33007. Sum of Differences
Sum of Differences
Sum of Differences
You are given a list of street numbers. These numbers are originally given in reverse order, but you need to compute the sum of differences between consecutive numbers after sorting them in ascending order.
Formally, let the list be represented as \(a_1, a_2, \dots, a_n\) (in reverse order). First, reverse the list (this undoes the reversal) and then sort it in ascending order to obtain \(b_1 \le b_2 \le \dots \le b_n\). The required answer is computed as:
\[ \sum_{i=2}^{n}(b_i - b_{i-1}) = b_n - b_1 \]
If there is only one number (or none), the answer is 0.
inputFormat
The input is read from stdin
and consists of two lines. The first line contains an integer n
which represents the number of street numbers. The second line contains n
space-separated integers representing the street numbers in reversed order.
outputFormat
Output the sum of differences between consecutive numbers (which is equivalent to the difference between the maximum and minimum number after sorting) to stdout
.
5
5 3 1 2 4
4