#K781. Can You Form an Arithmetic Progression?
Can You Form an Arithmetic Progression?
Can You Form an Arithmetic Progression?
You are given a sequence of n integers. Your task is to determine whether it is possible to rearrange the sequence so that it forms an arithmetic progression.
An arithmetic progression is a sequence where the difference between consecutive terms is constant. In mathematical notation, if the sorted sequence is \(a_1, a_2, \dots, a_n\), then there exists a constant \(d\) such that:
\(a_{i+1} - a_i = d \quad \text{for every } 1 \leq i < n\)
If the sequence can be rearranged to satisfy this condition, print YES
; otherwise, print NO
.
inputFormat
The input is given via standard input and consists of two lines.
- The first line contains a single integer \(n\) representing the number of elements in the sequence.
- The second line contains \(n\) space-separated integers, which are the elements of the sequence.
outputFormat
Output a single line containing YES
if it is possible to rearrange the sequence into an arithmetic progression, or NO
otherwise.
5
3 1 5 7 9
YES