#C13873. Complete Arithmetic Sequence Restoration
Complete Arithmetic Sequence Restoration
Complete Arithmetic Sequence Restoration
You are given a sequence of integers which represents an arithmetic progression. It is possible that exactly one number is missing from the complete sequence. Your task is to determine if the given sequence forms a complete arithmetic sequence. If not, find the missing number and restore the sequence into the complete arithmetic progression.
Recall that an arithmetic sequence is a sequence in which the difference between any two consecutive terms is constant. In other words, given an arithmetic sequence \(a_1, a_2, \dots, a_n\), the common difference \(d\) satisfies \[ a_{i} - a_{i-1} = d, \quad \forall\, 2 \le i \le n. \]
If the sequence is already complete (i.e. it is a valid arithmetic sequence), simply output the sequence as it is. Otherwise, compute the missing value using the formula for the sum of an arithmetic series: \[ S = \frac{(n+1)(a_1 + a_{n+1})}{2}, \] where \(n+1\) is the total number of terms in the complete sequence, and \(a_1\) and \(a_{n+1}\) are the first and last terms respectively. The missing number is then \(S - \text{sum}(\text{sequence})\).
Input/Output Requirement:
The input will be given via standard input (stdin) and the output should be printed to standard output (stdout).
inputFormat
The first line contains an integer \(n\) representing the number of elements in the provided sequence. The second line contains \(n\) space-separated integers representing the sequence. It is guaranteed that the sequence is sorted in non-decreasing order.
outputFormat
Output the complete arithmetic sequence (with the missing element inserted if needed) as space-separated integers in a single line.
## sample3
1 2 4
1 2 3 4
</p>