One and Two (Codeforces 1788A)
Tags: Math, brute force

You are given a sequence $$$a_1, a_2, \ldots, a_n$$$. Each element of $$$a$$$ is $$$1$$$ or $$$2$$$.Find out if an integer $$$k$$$ exists so that the following conditions are met.   $$$1 \leq k \leq n-1$$$, and  $$$a_1 \cdot a_2 \cdot \ldots \cdot a_k = a_{k+1} \cdot a_{k+2} \cdot \ldots \cdot a_n$$$. If there exist multiple $$$k$$$ that satisfy the given condition, print the smallest.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 1000$$$).The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2$$$).

Output: For each test case, if there is no such $$$k$$$, print $$$-1$$$.Otherwise, print the smallest possible $$$k$$$.

Note: For the first test case, $$$k=2$$$ satisfies the condition since $$$a_1 \cdot a_2 = a_3 \cdot a_4 \cdot a_5 \cdot a_6 = 4$$$. $$$k=3$$$ also satisfies the given condition, but the smallest should be printed.For the second test case, there is no $$$k$$$ that satisfies $$$a_1 \cdot a_2 \cdot \ldots \cdot a_k = a_{k+1} \cdot a_{k+2} \cdot \ldots \cdot a_n$$$For the third test case, $$$k=1$$$, $$$2$$$, and $$$3$$$ satisfy the given condition, so the answer is $$$1$$$.

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

Sample Outputs:
2
-1
1

Solution (Python):
def find_smallest_k(sequence, n):
    t_cnt = sequence.count(2)
    if t_cnt == 0:
        return 1
    elif t_cnt % 2 != 1:
        j = 0
        i = 0
        while i < t_cnt // 2:
            if sequence[j] == 2:
                i += 1
            j += 1
        return j
    return -1
for _ in range(int(input())):
    n = int(input())
    sequence = list(map(int, input().split()))
    result = find_smallest_k(sequence, n)
    print(result)

Solution (Python):
def solve():
    n = int(input())
    arr = list(map(int, input().split()))
    y = arr.count(2)
    if y % 2 == 0:
        y //= 2
        i = 0
        while y > 0:
            if arr[i] == 2:
                y -= 1
                arr[i] = 1
            else:
                i += 1
        print(i + 1)
    else:
        print(-1)
tests = int(input())
for _ in range(tests):
    solve()

Solution (Python):
def solve(ar):
    twos = ar.count(2)
    if twos % 2 != 0:
        return -1
    passed_twos = 0
    need_twos = twos // 2
    for (i, x) in enumerate(ar):
        if x == 2:
            passed_twos += 1
        if passed_twos == need_twos:
            return i + 1
t = int(input())
for _ in range(t):
    n = int(input())
    ar = list(map(int, input().split()))
    print(solve(ar))