Left and Right Houses (Codeforces 1945C)
Tags: Others, brute force

In the village of Letovo, there are $$$n$$$ houses. The villagers decided to build a big road that will divide the village into left and right sides. Each resident wants to live on either the right or the left side of the street, which is described as a sequence $$$a_1, a_2, \dots, a_n$$$, where $$$a_j = 0$$$ if the resident of the $$$j$$$-th house wants to live on the left side of the street; otherwise, $$$a_j = 1$$$.The road will pass between two houses. The houses to the left of it will be declared the left-side, and the houses to the right will be declared the right-side. More formally, let the road pass between houses $$$i$$$ and $$$i+1$$$. Then the houses at positions between $$$1$$$ and $$$i$$$ will be on the left side of the street, and at positions between $$$i+1$$$ and $$$n$$$ will be on the right side. The road also may pass before the first and after the last house; in this case, the entire village is declared to be either the right or left side, respectively.To make the design fair, it was decided to lay the road so that at least half of the residents on each side of the village are satisfied with the choice. That is, among $$$x$$$ residents on one side, at least $$$\lceil\frac{x}{2}\rceil$$$ should want to live on that side, where $$$\lceil x \rceil$$$ denotes rounding up a real number $$$x$$$.  To the left of the road, there will be $$$i$$$ houses, among the corresponding $$$a_j$$$ there must be at least $$$\lceil\frac{i}{2}\rceil$$$ zeros. To the right of the road, there will be $$$n-i$$$ houses, among the corresponding $$$a_j$$$ there must be at least $$$\lceil\frac{n-i}{2}\rceil$$$ ones. Determine after which house $$$i$$$ the road should be laid in order to satisfy the described condition and be as close to the middle of the village as possible. Formally, among all suitable positions $$$i$$$, minimize $$$\left|\frac{n}{2} - i\right|$$$.If there are multiple suitable positions $$$i$$$ with the minimum $$$\left|\frac{n}{2} - i\right|$$$, output the smaller one.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). The description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 3\cdot 10^5$$$). The next line of each test case contains a string $$$a$$$ of length $$$n$$$, consisting only of $$$0$$$ and $$$1$$$.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3\cdot 10^5$$$.

Output: For each test case, output a single number $$$i$$$ — the position of the house after which the road should be laid (if it should be laid before the first house, output $$$0$$$). We can show that the answer always exists.

Note: Let's consider the first example of input data.If we lay the road after the first house, there will be one house $$$a_1 = 1$$$ on the left side of the street, the resident of which would like to live on the right side of the street. Then $$$0$$$ out of $$$1$$$ residents on the even side will be satisfied with the choice, which means that the road cannot be laid after house $$$1$$$.If we lay the road after the second house, $$$1$$$ out of $$$2$$$ residents on the left side (with preferences $$$a_1 = 1$$$, $$$a_2 = 0$$$) and $$$1$$$ out of $$$1$$$ resident on the right side (with preference $$$a_3 = 1$$$) will be satisfied with the choice. More than half of the residents on each side are satisfied with the choice, which means that the road can be laid after house $$$2$$$. We can show that this is the optimal answer.

Sample Inputs:
7
3
101
6
010111
6
011001
3
000
3
110
3
001
4
1100

Sample Outputs:
2
3
2
3
0
1
0

Solution (Python):
""""""
import sys

def sol():
    input = sys.stdin.readline
    n = int(input())
    s = input()
    ans = 0
    r1 = s.count('1')
    r0 = n - r1
    l1 = 0
    l0 = 0
    mid = n / 2
    mmid = int(mid)
    if not (l0 >= l1 and r1 >= r0):
        ans = 10000000000
    fh = True
    sans = 100000000000
    for i in range(n):
        if s[i] == '1':
            r1 -= 1
            l1 += 1
        else:
            r0 -= 1
            l0 += 1
        if mmid == i:
            fh = False
        if l0 >= l1 and r1 >= r0:
            if fh:
                if ans == 10000000000:
                    ans = i + 1
                else:
                    ans = max(ans, i + 1)
            else:
                sans = min(sans, i + 1)
    if sans - mid < mid - ans or ans == 10000000000:
        print(sans)
    else:
        print(ans)
for _ in range(int(input())):
    sol()

Solution (Python):
for s in [*open(0)][2::2]:
    n = len((s := s[:-1]))
    a = [(t := 0)] + [(t := (t + 2 * int(x))) for x in s]
    print(min(((abs(n - i * 2), i) for (i, x) in enumerate(a) if 0 <= i - x >= n - a[-1]))[1])

Solution (Python):
for s in [*open(0)][2::2]:
    t = 0
    n = len((a := [(t := (t + 2 * int(x))) for x in s[:-1]]))
    print(min(((abs(n - i * 2), i) for (i, x) in enumerate([0] + a) if 0 <= i - x >= n - t))[1])