time limit per test
1 second
memory limit per test
256 megabytes
An array b1,b2,…,bnb1,b2,…,bn of positive integers is good if all the sums of two adjacent elements are equal to the same value. More formally, the array is good if there exists a kk such that b1+b2=b2+b3=…=bn−1+bn=kb1+b2=b2+b3=…=bn−1+bn=k.
Doremy has an array aa of length nn. Now Doremy can permute its elements (change their order) however she wants. Determine if she can make the array good.
Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer nn (2≤n≤1002≤n≤100) — the length of the array aa.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1051≤ai≤105).
There are no constraints on the sum of nn over all test cases.
Output
For each test case, print "Yes" (without quotes), if it is possible to make the array good, and "No" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
Example
Input
Copy
5
2
8 9
3
1 1 2
4
1 1 4 5
5
2 3 3 3 3
4
100000 100000 100000 100000
Output
Copy
Yes Yes No No Yes
Note
In the first test case, [8,9][8,9] and [9,8][9,8] are good.
In the second test case, [1,2,1][1,2,1] is good because a1+a2=a2+a3=3a1+a2=a2+a3=3.
In the third test case, it can be shown that no permutation is good.
解题说明:此题是一道数学题,找规律能发现要么所有数的值相等,要么存在两种数,个数相差1。可以采用map来统计个数进行判断。
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
using namespace std;int t, n, a[102];
map<int, int>mp;
int main()
{scanf("%d", &t);while (t--){scanf("%d", &n);mp.clear();for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);mp[a[i]]++;}if (mp.size() <= 1){printf("YES\n");}else if (mp.size() == 2) {int a = 0, b = 0;for (auto it : mp) {if (!a){a = it.second;}else{b = it.second;}}if (abs(a - b) <= 1){printf("YES\n");}else{printf("NO\n");}}else{printf("NO\n");}}return 0;
}