Menorah (Codeforces 1615C)
Tags: Greedy, graphs

There are $$$n$$$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $$$s$$$, where the $$$i$$$-th candle is lit if and only if $$$s_i=1$$$.  Initially, the candle lights are described by a string $$$a$$$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit).You would like to make the candles look the same as string $$$b$$$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required.

Input: The first line contains an integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow.The first line of each test case contains a single integer $$$n$$$ ($$$1\le n\le 10^5$$$) — the number of candles.The second line contains a string $$$a$$$ of length $$$n$$$ consisting of symbols 0 and 1 — the initial pattern of lights.The third line contains a string $$$b$$$ of length $$$n$$$ consisting of symbols 0 and 1 — the desired pattern of lights.It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^5$$$.

Output: For each test case, output the minimum number of operations required to transform $$$a$$$ to $$$b$$$, or $$$-1$$$ if it's impossible.

Note: In the first test case, the two strings are already equal, so we don't have to perform any operations.In the second test case, we can perform a single operation selecting the second candle to transform $$$01$$$ into $$$11$$$.In the third test case, it's impossible to perform any operations because there are no lit candles to select.In the fourth test case, we can perform the following operations to transform $$$a$$$ into $$$b$$$:   Select the $$$7$$$-th candle: $$$100010{\color{red}1}11\to 011101{\color{red} 1}00$$$.  Select the $$$2$$$-nd candle: $$$0{\color{red} 1}1101100\to 1{\color{red} 1}0010011$$$.  Select the $$$1$$$-st candle: $$${\color{red}1}10010011\to {\color{red}1}01101100$$$. In the fifth test case, we can perform the following operations to transform $$$a$$$ into $$$b$$$:   Select the $$$6$$$-th candle: $$$00101{\color{red}1}011\to 11010{\color{red}1}100$$$  Select the $$$2$$$-nd candle: $$$1{\color{red}1}0101100\to 0{\color{red}1}1010011$$$  Select the $$$8$$$-th candle: $$$0110100{\color{red}1}1\to 1001011{\color{red}1}0$$$  Select the $$$7$$$-th candle: $$$100101{\color{red}1}10\to 011010{\color{red}1}01$$$ 

Sample Inputs:
5
5
11010
11010
2
01
11
3
000
101
9
100010111
101101100
9
001011011
011010101

Sample Outputs:
0
1
-1
3
4

Solution (Python):
import bisect
import collections
import math
import sys
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.buffer.readline())

def MI():
    return map(int, sys.stdin.buffer.readline().split())

def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def BI():
    return sys.stdin.buffer.readline().rstrip()

def SI():
    return sys.stdin.buffer.readline().rstrip().decode()

def li():
    return [int(i) for i in input().split()]

def lli(rows):
    return [li() for _ in range(rows)]

def si():
    return input()

def ii():
    return int(input())

def ins():
    return input().split()

def drank(d, processing, da, rank):
    tmp = 10 ** 9
    if len(d[da]) == 1:
        return 1
    for di in d[da]:
        if processing[di - 1] == 0:
            processing[di - 1] = 1
            tmp = min(tmp, drank(d, processing, di, rank))
            processing[di - 1] = 0
    rank[da - 1] = tmp + 1
    return tmp + 1

def binary_search(n, a):
    l = len(a)
    low = 0
    high = l - 1
    while high >= low:
        mid = (high + low) // 2
        if a[mid] == n:
            return mid
        elif a[mid] > n:
            high = mid - 1
        else:
            low = mid + 1
    return -1
is_prime = [True] * (10 ** 6 + 1)

def primeseive(n):
    is_prime[0] = is_prime[1] = False
    i = 2
    while i * i <= n:
        if is_prime[i] == True:
            for j in range(i * i, n + 1, i):
                is_prime[j] = False
        i += 1

def solve():
    n = II()
    a = SI()
    b = SI()
    similar = 0
    onesimilar = 0
    onedisimilar = 0
    for i in range(n):
        if a[i] == b[i]:
            similar += 1
            if a[i] == '1':
                onesimilar += 1
        elif a[i] == '1':
            onedisimilar += 1
    disimilar = n - similar
    zerosimilar = similar - onesimilar
    zerodisimilar = disimilar - onedisimilar
    x = n * 100
    y = n * 100
    if onesimilar - zerosimilar == 1:
        x = similar
    if onedisimilar == zerodisimilar:
        y = disimilar
    if x == y == n * 100:
        return -1
    else:
        return min(x, y)

def main():
    for _ in range(II()):
        sys.stdout.write(str(solve()) + '\n')
if __name__ == '__main__':
    main()

Solution (Python):
res = []
for i in range(int(input())):
    n = int(input())
    a = input()
    b = input()
    a1 = a.count('1')
    b1 = b.count('1')
    diff = 0
    val1 = val2 = float('inf')
    for i in range(len(a)):
        if a[i] != b[i]:
            diff += 1
    if a1 == b1:
        val1 = diff
    if b1 == n - a1 + 1:
        val2 = n - diff
    if val1 == val2 == float('inf'):
        res.append(-1)
    else:
        res.append(min(val1, val2))
for val in res:
    print(val)

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

def solve():
    n = int(input())
    a = input().strip()
    b = input().strip()
    diff = cnt1 = cnt2 = 0
    for i in range(n):
        cnt1 += int(a[i])
        cnt2 += int(b[i])
        diff += 1 if a[i] != b[i] else 0
    ans = float('inf')
    if cnt1 == cnt2:
        ans = min(ans, diff)
    if cnt1 + cnt2 == n + 1:
        if (n - diff) % 2 == 1:
            ans = min(ans, n - diff)
    return ans if ans < float('inf') else -1
for _ in range(int(input())):
    print(solve())