-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholliePS-237.cpp
122 lines (114 loc) · 2.35 KB
/
olliePS-237.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ull unsigned long long
#define flash() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const long long int MOD = 1e9 + 7;
const long long int INF = 1e18;
const long long int MAX = 1e9;
void solution()
{
ll n;
cin >> n;
vector<ll> v;
for(ll i = 1 ; i <= n ; i++)
{
ll x;
cin >> x;
v.push_back(x);
}
ll start = 0 , end = n-1 , defect = -1;
bool check1 = true , check2 = true;
while(start <= end)
{
if(v[start] == v[end])
{
start++;
end--;
continue;
}
if(v[start] != v[end] && defect == -1)
{
defect = v[start];
start++;
continue;
}
if(v[start] != v[end] && defect != -1)
{
if(v[start] == defect && v[end] != defect)
{
start++;
continue;
}
if(v[end] == defect && v[start] != defect)
{
end--;
continue;
}
if(v[start] != defect && v[end] != defect)
{
check1 = false;
break;
}
}
}
if(check1 == true)
{
cout << "YES" << endl;
return;
}
start = 0 , end = n-1 , defect = -1;
while(start <= end)
{
if(v[start] == v[end])
{
start++;
end--;
continue;
}
if(v[start] != v[end] && defect == -1)
{
defect = v[end];
end--;
continue;
}
if(v[start] != v[end] && defect != -1)
{
if(v[start] == defect && v[end] != defect)
{
start++;
continue;
}
if(v[end] == defect && v[start] != defect)
{
end--;
continue;
}
if(v[start] != defect && v[end] != defect)
{
check2 = false;
break;
}
}
}
if(check2 == true)
{
cout << "YES" << endl;
}
else
{
cout << "NO" <<endl;
}
return;
}
int main()
{
flash();
ll t;
cin >> t;
while(t--)
{
solution();
}
return 0;
}