-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathD.cpp
50 lines (45 loc) · 1.02 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
bool check(int idx,vector<int>a){
a.erase(a.begin()+idx);
vector<int> v1;
for(int i=0; i<a.size()-1; i++){
v1.push_back(__gcd(a[i],a[i+1]));
}
if(is_sorted(v1.begin(),v1.end())) return true;
else return false;
}
int main()
{
int t;
cin>>t;
while (t--)
{
int n;
cin>>n;
vector<int> a(n);
for(int i=0; i<n; i++){cin>>a[i];}
vector<int> v;
for(int i=0; i<n-1; i++){
v.push_back(__gcd(a[i],a[i+1]));
}
if(is_sorted(v.begin(),v.end())){
cout<<"YES"<<endl;
continue;
}
int idx=0;
for(int i=0; i<v.size()-1; i++){
if(v[i]>v[i+1]){
idx=i;
break;
}
}
if(check(idx,a)|| check(idx+1,a)||check(idx+2,a)){
cout<<"YES"<<endl;
}
else cout<<"NO"<<endl;
}
return 0;
}