CF BUDDY
← Problems·

1955E · Long Inversions

1700 · brute force, greedy, implementation

Problem: A binary string ss of length nn is given. A binary string is a string consisting only of the characters '1' and '0'.

You can choose an integer kk (1kn1 \le k \le n) and then apply the following operation any number of times: choose kk consecutive characters of the string and invert them, i.e., replace all '0' with '1' and vice versa.

Using these operations, you need to make all the characters in the string equal to '1'.

For example, if n=5n=5, s=00100s=00100, you can choose k=3k=3 and proceed as follows:

  • choose the substring from the 11-st to the 33-rd character and obtain s=11000s=\color{blue}{110}00;
  • choose the substring from the 33-rd to the 55-th character and obtain s=11111s=11\color{blue}{111};

Find the maximum value of kk for which it is possible to make all the characters in the string equal to '1' using the described operations. Note that the number of operations required to achieve this is not important.

Input Format: The first line contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains an integer nn (1n50001 \le n \le 5000) — the length of the string ss.

The second line of each test case contains a string ss of length nn, consisting of the characters '1' and '0'.

It is guaranteed that the sum of the values n2n^2 over all test cases in the test does not exceed 2510625 \cdot 10^6.

Output Format: For each test case, output the maximum integer kk (1kn1 \le k \le n) for which it is possible to obtain a string ss consisting only of the characters '1' using the described operations.

Sample Cases

Case 1

Input

5
5
00100
5
01000
7
1011101
3
000
2
10

Output

3
2
4
3
1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback