#K46997. Longest Consecutive Ones After a Single Flip

    ID: 28100 Type: Default 1000ms 256MiB

Longest Consecutive Ones After a Single Flip

Longest Consecutive Ones After a Single Flip

You are given a binary string S of length N. You are allowed to flip exactly one '0' to a '1'. After this operation, the contiguous sequence of '1's may merge with adjacent sequences of '1's. Formally, if the chosen '0' is between two segments of consecutive '1's with lengths L1 and L2, then the resulting contiguous block will have length \(L1+1+L2\). If there is no '0' in the string, the answer is simply N.

Your task is to determine the maximum possible length of a contiguous segment of '1's after performing at most one flip.

inputFormat

The first line of the input contains an integer T denoting the number of test cases. Each test case consists of a single line containing an integer N and a binary string S of length N separated by a space.

For example:

3
3 101
5 11010
6 000000

outputFormat

For each test case, output one line containing an integer – the maximum length of the contiguous segment of '1's that can be obtained after flipping exactly one '0'.

For the sample input above, the output should be:

3
4
1
## sample
3
3 101
5 11010
6 000000
3

4 1

</p>