April Long Two 2022 Division 4 (Rated)

 


Geometric Mean Inequality

You are given an array A of length N containing the elements 1 and 1 only. Determine if it is possible to rearrange the array A in such a way that Ai is not the geometric mean of Ai1 and Ai+1, for all i such that 2iN1.

Y is said to be the geometric mean of X and Z if Y2=XZ.

Input Format

  • The first line contains a single integer T - the number of test cases. Then the test cases follow.
  • The first line of each test case contains an integer N - the size of the array A.
  • The second line of each test case contains N space-separated integers A1,A2,,AN denoting the array A.

Output Format

For each test case, output Yes if it is possible to rearrange A in such a way that Ai is not the geometric mean of Ai1 and Ai+1, where 2iN1. Otherwise output No.

You may print each character of Yes and No in uppercase or lowercase (for example, yesyEsYES will be considered identical).

Constraints

  • 1T200
  • 3N1000
  • Ai{1,1}

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 A to [1,1,1,1,1]. One can see that Ai2Ai1Ai+1, for any 2iN1.

Test case 2: None of the rearrangements of A 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;

}