Ankit_132

D

Jun 3rd, 2024
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. const int N=2e5+5;
  6. int n;
  7. int a[N],b[N],c[N];
  8.  
  9. bool check(int x){
  10.     int t=0;
  11.  
  12.     for(int i=1;i<=n;i++){
  13.         if(i==x) continue;
  14.         c[++t]=a[i];
  15.     }
  16.  
  17.     int last=1;
  18.     for(int i=1;i<n-1;i++){
  19.         int y=__gcd(c[i],c[i+1]);
  20.         if(y<last) return false;
  21.         last=y;
  22.     }
  23.     return true;
  24. }
  25.  
  26. int main(){
  27.     int t;
  28.     cin>>t;
  29.  
  30.     while(t--){
  31.         cin>>n;
  32.  
  33.         for(int i=1;i<=n;i++) cin>>a[i];
  34.         for(int i=1;i<n;i++) b[i]=__gcd(a[i],a[i+1]);
  35.  
  36.         int x=-1;
  37.  
  38.         for(int i=1;i<n-1;i++){
  39.             if(b[i]>b[i+1]){
  40.                 x=i;
  41.                 break;
  42.             }
  43.         }
  44.  
  45.         if(x==-1)
  46.             cout<<"YES\n";
  47.         else if(check(x)||check(x+1)||check(x+2))
  48.             cout<<"YES\n";
  49.         else
  50.             cout<<"NO\n";
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment