Title description
Compared to Gugudong, Ruishen is a good kid who got greedy early this morning. Ruishen got up early this morning and saw a sequence a while brushing station B. He became very interested in this sequence. He wonders whether there is a number K, so that some numbers are added to K, some numbers are subtracted from K, and some numbers are unchanged, so that all numbers in the entire sequence are equal. Among them, the number at each position in the sequence can only be added or subtracted at most once or no operation is performed on the position. Since Raytheon only brushes station B, he gave you this question!
Input format
The first line of input is a positive integer tt indicating the number of data groups. Next, for each set of data, the first positive integer n entered represents the length of sequence a, and there are n integers on the following line, representing sequence a.
Output format
The output contains a total of t lines, one line for each set of data. For each set of data, if there is such a K, output "YES", otherwise output "NO". (The output does not contain quotation marks)
Sample input
2
5
1 2 3 4 5
5
1 2 3 4 5
Sample output
NO
NO
Ideas
Store each input number in the array and then sort, traverse the sorted array from left to right, record the first three different numbers and the number cnt, if more than three, stop directly. This question is really a pit. I misunderstood the meaning of the question, thinking that there are only three different numbers, and only output OK when max-mid = mid-min, plus OK when cnt <2, and NO in other cases .
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
int t,n,cnt;
ll a[10005],s[3];
int main()
{
scanf("%d",&t);
while (t--)
{
cnt=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lld",&a[i]);
sort(a,a+n);
s[0]=a[0];
for(int i=1;i<n;i++)
{
if(a[i]!=a[i-1])
{
cnt++;
s[cnt]=a[i];
if(cnt>2)
{
break;
}
}
}
if(cnt<2)cout<<"YES"<<endl;// 忽略了一种情况,菜
else if(cnt==2&&(s[1]-s[0]==s[2]-s[1]))
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
// system("pause");
return 0;
}