#K11126. Missing Number in Arithmetic Progression
Missing Number in Arithmetic Progression
Missing Number in Arithmetic Progression
You are given a list of values that represent an arithmetic progression with exactly one term missing. An arithmetic progression is a sequence of numbers in which the difference between consecutive terms is constant. Your task is to find and output the missing number that should be present in the sequence to maintain its arithmetic progression. If the input is invalid (for example, if it contains non-integer values or has fewer than three numbers), output invalid input
.
Hint: In a valid arithmetic progression with one missing number, the common difference \(d\) can be computed by \(d = \frac{a_n - a_1}{n}\) where \(a_1\) is the first term, \(a_n\) is the last term, and \(n\) is the total number of terms that would be present if no term was missing. Use this to identify the anomaly in the sequence.
inputFormat
The input is provided via standard input.
It consists of a single line containing space-separated tokens representing the arithmetic progression. Each token is intended to be an integer. For example:3 6 9 15
outputFormat
Output the missing number to standard output. In case the input is invalid, output invalid input
.
3 6 9 15
12