Berry Jam (Codeforces 1278C)
Tags: Greedy, data structures

Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $$$2n$$$ jars of strawberry and blueberry jam.All the $$$2n$$$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $$$n$$$ jars to his left and $$$n$$$ jars to his right.For example, the basement might look like this:  Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right.Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same.For example, this might be the result:  He has eaten $$$1$$$ jar to his left and then $$$5$$$ jars to his right. There remained exactly $$$3$$$ full jars of both strawberry and blueberry jam. Jars are numbered from $$$1$$$ to $$$2n$$$ from left to right, so Karlsson initially stands between jars $$$n$$$ and $$$n+1$$$.What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left?Your program should answer $$$t$$$ independent test cases.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \dots, a_{2n}$$$ ($$$1 \le a_i \le 2$$$) — $$$a_i=1$$$ means that the $$$i$$$-th jar from the left is a strawberry jam jar and $$$a_i=2$$$ means that it is a blueberry jam jar.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: For each test case print the answer to it — the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left.

Note: The picture from the statement describes the first test case.In the second test case the number of strawberry and blueberry jam jars is already equal.In the third test case Karlsson is required to eat all $$$6$$$ jars so that there remain $$$0$$$ jars of both jams.In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $$$1$$$ jar of both jams.

Sample Inputs:
4
6
1 1 1 2 2 1 2 1 2 1 1 2
2
1 2 1 2
3
1 1 1 1 1 1
2
2 1 1 1

Sample Outputs:
6
0
6
2

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    l1 = input().split()
    straw = 0
    blue = 0
    for i in l1:
        if i == '1':
            straw += 1
        else:
            blue += 1
    inc = 0
    if straw == blue:
        print(0)
        continue
    elif straw > blue:
        inc = '1'
        target = straw - blue
    else:
        inc = '2'
        target = blue - straw
    d = {0: 0}
    ca = 0
    jars = 0
    for i in l1[n - 1:-2 * n - 1:-1]:
        jars += 1
        if i == inc:
            ca += 1
        else:
            ca -= 1
        if ca in d:
            pass
        else:
            d[ca] = jars
    ca = 0
    jars = 0
    ans = 2 * n
    if target - ca in d:
        ans = min(jars + d[target - ca], ans)
    for i in l1[n:]:
        jars += 1
        if i == inc:
            ca += 1
        else:
            ca -= 1
        if target - ca in d:
            ans = min(jars + d[target - ca], ans)
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, arr) = (readInt(), readList())
    arr = [v if v == 1 else -1 for v in arr]
    if sum(arr) < 0:
        arr = [-v for v in arr]
    t = sum(arr)
    ans = 2 * n
    if t == 0:
        return 0
    else:
        idx = [10 ** 10] * (n + 1)
        curr = 0
        for i in range(n, 2 * n):
            curr += arr[i]
            if curr >= 0:
                idx[curr] = min(idx[curr], i)
        if t <= n:
            ans = min(ans, idx[t] - n + 1)
        curr = 0
        for i in range(n - 1, -1, -1):
            curr += arr[i]
            if curr == t:
                ans = min(ans, n - i)
            elif 0 < t - curr <= n:
                ans = min(ans, idx[t - curr] - i + 1)
        return ans
for _ in range(int(input())):
    print(solve())

Solution (Python):
t = int(input())

def solve(t):
    n = int(input())
    a = list(map(int, input().split()))
    a.append(1)
    d = {0: -1}
    t = 0
    for i in range(n):
        if a[i] == 1:
            t += 1
        d[i + 1 - 2 * t] = i
    ans = 10 ** 9
    t = -1
    for i in reversed(range(n, 2 * n + 1)):
        if a[i] == 1:
            t += 1
        cur = 2 * n - i - 2 * t
        ans = min(ans, i - d.get(-cur, -10 ** 9))
    print(ans - 1)
for i in range(t):
    solve(i + 1)