#C874. Check Arithmetic Progression
Check Arithmetic Progression
Check Arithmetic Progression
Given an array of integers, determine whether the numbers can be rearranged into an arithmetic progression (AP). An arithmetic progression is a sequence in which the difference between consecutive elements is constant. For example, the array [3, 5, 1, 7] can be rearranged as [1, 3, 5, 7] which is an AP with common difference 2, while [1, 2, 4, 7] cannot form an AP.
Your task is to write a program that reads an integer n followed by n integers from stdin and prints True
if the array can form an arithmetic progression, or False
otherwise. Note that an empty array or a single element array is considered to be an arithmetic progression.
The formula for an arithmetic progression is given by:
\(a,\; a+d,\; a+2d,\; \dots,\; a+(n-1)d\)
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains a single integer
n
(\(n \ge 0\)), representing the number of elements in the array. - The second line contains
n
space-separated integers.
If n
is 0, the second line may be omitted.
outputFormat
Output a single line to stdout containing either True
or False
depending on whether the array can be rearranged into an arithmetic progression.
4
3 5 1 7
True