#C5790. Arithmetic Progression Rearrangement
Arithmetic Progression Rearrangement
Arithmetic Progression Rearrangement
You are given an integer array. Your task is to determine if the array can be rearranged into an arithmetic progression. An arithmetic progression is a sequence of numbers where the difference between consecutive terms is constant. In mathematical notation, for an arithmetic progression \(a_1, a_2, \dots, a_n\), the condition is:
\(a_{i+1} - a_i = d\) for all \(1 \leq i < n\)
If the array can be rearranged such that it satisfies the above condition, print YES
; otherwise, print NO
.
Note: For an array with only two elements or less, it is always considered as forming an arithmetic progression.
inputFormat
The first line contains an integer \(T\) representing the number of test cases. Each test case consists of two lines. The first line of each test case contains an integer \(n\) denoting the number of elements in the array. The second line contains \(n\) space-separated integers representing the array.
Example:
3 3 3 5 1 4 1 4 2 3 4 1 2 4 7
outputFormat
For each test case, output a single line containing either YES
if the array can be rearranged to form an arithmetic progression, or NO
otherwise.
Example Output:
YES YES NO## sample
3
3
3 5 1
4
1 4 2 3
4
1 2 4 7
YES
YES
NO
</p>