-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question8.cpp
180 lines (144 loc) · 4.68 KB
/
Question8.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Meenu has a binary string S of length N.
In one operation, Meenu can:
Select two indices i and j (1≤i,j≤N) and toggle Si & Sj
For example, if S = 100101 and Meenu applys operation on i = 1 and j = 3 she get's
001101.
Find if it is possible to convert S to such a string which is read same in both the direction such string is called "SPECIAL"
Input Format must be this
The first line of each test case contains an integer N — the length of the binary string S.
The second line of each test case contains a binary string S of length N containing 0s and 1s only.
Output Format must be this
For each test case, output YES if it is possible to convert S to a SPECIAL string. Otherwise, output NO.
Constraints
1<=T<=10^3
1<=N<=10^3
S is a binary string
Input
3
6
101011
2
01
7
1110000
Output
YES
NO
YES
*/
/*
__________________________
| Written by silent_Joy |
---------~u~---------
*/
#include"bits/stdc++.h"
using namespace std;
/*____________________________________MACRO DEFINATIONS_______________________________________*/
#define ul unsigned long int // 0 to 2^32 - 1
#define ll long long // -2^63 to 2^63 - 1
#define ull unsigned long long // 0 to 2^64 - 1
#define ld long double // 12 bytes (12*8 bits)
//vector macro
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int> >
#define vvll vector<vector<long, long> >
//map macro
#define mii map<int, int>
#define mis map<int, string>
#define mss map<string, string>
#define msi map<string, int>
//pair macro
#define pii pair<int, int>
#define pis pair<int, string>
#define pss pair<string, string>
//priority queue
#define mxheap priority_queue<ll>
#define mnheap priority_queue<ll,vector<ll>,greater<ll>>
typedef char* ptr;
//some useful functions as macro
#define len(str) str.length();
#define all(a) a.begin(), a.end()
#define allrev(a) a.rbegin(), a.rend()
#define endl "\n"
#define watch(x) cout << #x << " = " << x << endl //simple debugger
//loop constructs shortened using macro defination
#define fori(a, n) for (ul i = a; i < (ul)n; i++)
#define forj(a, n) for (ul j = a; j < (ul)n; j++)
#define fork(a, n) for (ul k = a; k < (ul)n; k++)
//note in place of decrement please put negative values only
#define foridec(n, a, dec) for (ul i = n; i > (ul)a; i+=dec)
#define forjdec(n, a, dec) for (ul j = n; j > (ul)a; j+=dec)
#define forkdec(n, a, dec) for (ul k = n; k > (ul)a; k+=dec)
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
/*____________________________________CONSTANTS FREQUENTLY USED____________________________________*/
const int mod = 1000000007;//10^9+7
const ll PI = 3.141592653589793238462;
/*_______________________________ FUNCTION DEFINATIONS ___________________________________*/
template <typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
bool isPowerOfTwo(T x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
template <typename T_all>//string,float,int,.... all types
bool isPalindrome(T_all s)
{
string new_s;
new_s = s;
int n = len(new_s);
fori(0,n/2)
{
if (new_s[i] != new_s[n - i - 1])
return false;
}
return true;
}
template<typename T> void _do(T x){cerr << x << "\n";}
template<typename T, typename ...U> void _do(T x, U ...y){cerr << x << ", "; _do(y...);}
#define dbg(...) cerr << #__VA_ARGS__ << " = "; _do(__VA_ARGS__);
// use the inbuilt function of C++ std::__gcd(x,y) (2 underscores)
ll lcm(ll x, ll y){return x*y/std::__gcd(x,y);} // LCM OF TWO NUMBERS
/*_________________________________WRITE YOUR CODE FOR EACH TEST CASE BELOW____________________________________*/
void test(){
int n;
string s;
cin >> n >> s;
/*
* Logic:-
*
* Counts no. of 0s and 1s.
* case 1: when n is odd
* always possible
*
* case 2: when n is even
*only when no. of 0s and 1s is even then only possible
*
*/
if(n%2) cout << "YES\n";
else{
int c1=0,c0 = 0;
for(char i : s){
if(i == '1') c1++;
else c0++;
}
if(c1%2==0 && c0%2==0) cout << "YES\n";
else cout << "NO\n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1;//if test case is 1 only please comment out cin statement below
cin >> t;
auto t1 = std::chrono::high_resolution_clock::now();
while(t--)
test();
auto t2 = std::chrono::high_resolution_clock::now();
#ifndef ONLINE_JUDGE
std::chrono::duration<double, std::milli> ms_double = t2 - t1;
cout << ms_double.count() << "ms\n";
#endif
return 0;
}