Difficulty:1626
Expand

Learn the building blocks of programming languages
Take our free programming courses and learn to solve problems like these.
Start Learning

Tactical Conversion

You are given a binary string SS of length NN, i.e. each character of SS is either 00 or 11.

You would like to convert every character of SS into 00.
To achieve this, you can perform the following operation:

  • Choose an index ii (1iN)(1 \le i \le N) such that Si=1S_i = 1, and change SiS_i to 00.

However, there is one restriction:
You cannot perform two consecutive operations on adjacent indices.
That is, if you operate on the sequence of indices i1,i2,,iki_1, i_2, \ldots, i_k, then for each 1j<k1 \le j \lt k the condition ijij+1>1|i_j - i_{j+1}| \gt 1 must hold.

Determine whether it is possible to make the entire string consist of only zeros under these conditions.

Input Format

  • The first line of input contains a single integer TT, denoting the number of test cases.
  • Each test case consists of two lines of input:
    • The first line contains a single integer NN — the length of the binary string.
    • The second line contains the binary string SS of length NN, consisting only of characters 0 and 1.

Output Format

For each test case, output a single string on a new line — YES if it is possible to convert the entire string to all zeros under the given rule, or NO otherwise.

You may print each character of the string in uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).

Constraints

  • 1T21051 \leq T \leq 2\cdot 10^5
  • 1N21051 \leq N \leq 2\cdot 10^5
  • SS is a binary string.
  • The sum of NN over all test cases won't exceed 21052\cdot 10^5.

Sample 1:

Input
Output
5
2
00
3
001
3
101
3
111
2
11
Yes
Yes
Yes
No
No

Explanation:

Test case 11: No operations are needed.

Test case 22: There is a single 11 at position 33.
Simply perform one operation with i=3i = 3, and the string becomes 000000 as desired.

Test case 33: There are two 11's, at positions 11 and 33.
Perform one operation with i=1i = 1, and the next operation with i=3i = 3, and we're done.

Test case 44: There are three ones, at positions 1,2,31, 2, 3.
It's not possible to operate on all of them, because:

  • If our first operation is on index 11, the second operation cannot be index 22 and so must be index 33.
    But then after index 33 we cannot operate on index 22 anyway, so that index will continue to contain a 11.
  • Similarly, the first operation being on index 33 will leave us unable to operate on index 22.
  • Finally, if the first operation is on index 22, then the second operation cannot be on either index 11 or index 33 since they're both adjacent to it.

Thus, there's no way to make everything 00.


More Info
Time limit1 secs
Memory limit1.5 GB
Source Limit50000 Bytes

Author(s)
Tester(s)
Editorialist(s)
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
}
 
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Visualize Code