Geometric Mean Inequality
You are given an array of length containing the elements and only. Determine if it is possible to rearrange the array in such a way that is not the geometric mean of and , for all such that .
is said to be the geometric mean of and if .
Input Format
- The first line contains a single integer - the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer - the size of the array .
- The second line of each test case contains space-separated integers denoting the array .
Output Format
For each test case, output Yes
if it is possible to rearrange in such a way that is not the geometric mean of and , where . Otherwise output No
.
You may print each character of Yes
and No
in uppercase or lowercase (for example, yes
, yEs
, YES
will be considered identical).
Constraints
Sample Input 1
3
5
1 1 1 -1 -1
3
1 1 1
6
1 -1 -1 -1 -1 1
Sample Output 1
Yes
No
Yes
Explanation
Test case 1: We can rearrange the array to . One can see that , for any .
Test case 2: None of the rearrangements of satisy the given condition.
Solution :
#include <iostream>
using namespace std;
int yes(){
cout<<"YES"<<endl;
return 0;
}
int no(){
cout<<"NO"<<endl;
return 0;
}
int main() {
int t; cin>>t;
int n;
while(t--){
cin>>n;
int a[n];
int b=0,c=0;
for(int i=0; i<n;i++){
cin>>a[i];
if(a[i]==-1) b++; else c++;
}
int r=abs(b-c);
if(r<2) yes();
else if(r==2) {
if(b%2) no();
else yes();
}
else no();
}
return 0;
}