Make Even (Codeforces 1611A)
Tags: Math, constructive algorithms

Polycarp has an integer $$$n$$$ that doesn't contain the digit 0. He can do the following operation with his number several (possibly zero) times: Reverse the prefix of length $$$l$$$ (in other words, $$$l$$$ leftmost digits) of $$$n$$$. So, the leftmost digit is swapped with the $$$l$$$-th digit from the left, the second digit from the left swapped with ($$$l-1$$$)-th left, etc. For example, if $$$n=123456789$$$ and $$$l=5$$$, then the new value of $$$n$$$ will be $$$543216789$$$.Note that for different operations, the values of $$$l$$$ can be different. The number $$$l$$$ can be equal to the length of the number $$$n$$$ — in this case, the whole number $$$n$$$ is reversed.Polycarp loves even numbers. Therefore, he wants to make his number even. At the same time, Polycarp is very impatient. He wants to do as few operations as possible.Help Polycarp. Determine the minimum number of operations he needs to perform with the number $$$n$$$ to make it even or determine that this is impossible.You need to answer $$$t$$$ independent test cases.

Input: The first line contains the number $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.Each of the following $$$t$$$ lines contains one integer $$$n$$$ ($$$1 \le n < 10^9$$$). It is guaranteed that the given number doesn't contain the digit 0.

Output: Print $$$t$$$ lines. On each line print one integer — the answer to the corresponding test case. If it is impossible to make an even number, print -1.

Note: In the first test case, $$$n=3876$$$, which is already an even number. Polycarp doesn't need to do anything, so the answer is $$$0$$$.In the second test case, $$$n=387$$$. Polycarp needs to do $$$2$$$ operations: Select $$$l=2$$$ and reverse the prefix $$$\underline{38}7$$$. The number $$$n$$$ becomes $$$837$$$. This number is odd. Select $$$l=3$$$ and reverse the prefix $$$\underline{837}$$$. The number $$$n$$$ becomes $$$738$$$. This number is even.It can be shown that $$$2$$$ is the minimum possible number of operations that Polycarp needs to do with his number to make it even.In the third test case, $$$n=4489$$$. Polycarp can reverse the whole number (choose a prefix of length $$$l=4$$$). It will become $$$9844$$$ and this is an even number.In the fourth test case, $$$n=3$$$. No matter how hard Polycarp tried, he would not be able to make an even number.

Sample Inputs:
4
3876
387
4489
3

Sample Outputs:
0
2
1
-1

Solution (Python):
def BoogeyMan() -> None:
    """"""
    for _ in range(ii()):
        n = si()
        if int(n) % 2 == 0:
            p(0)
        else:
            l = []
            for (idx, i) in enumerate(n):
                if int(i) % 2 == 0:
                    l.append(idx)
            if len(l) == 0:
                p(-1)
            elif l[0] == 0:
                p(1)
            else:
                p(2)
        try:
            L(l)
        except:
            pass
if __name__ == '__main__':
    import os, sys, math, itertools, bisect
    from collections import deque, defaultdict, OrderedDict, Counter
    (ii, si) = (lambda : int(input()), lambda : input())
    (mi, msi) = (lambda : map(int, input().strip().split(' ')), lambda : map(str, input().strip().split(' ')))
    (li, lsi) = (lambda : list(mi()), lambda : list(msi()))
    (out, export, p, pp) = ([], lambda : print('\n'.join(map(str, out))), lambda x: out.append(x), lambda array: p(' '.join(map(str, array))))
    try:
        from baba_yaga import L, cmdIO, _generator_

        class _BoogeyMan_:

            def __init__(self) -> None:
                self.launchers = [cmdIO(), BoogeyMan(), export(), _generator_()]

            def init(self) -> None:
                for _ in self.launchers:
                    yield _
                    assert not _, 'EOF'

            def container(fun):

                def wrapper():
                    print(f'Success', end='\n')
                    fun()
                return wrapper

        @_BoogeyMan_.container
        def assembler():
            _BoogeyMan_().init()
        assembler()
    except (FileNotFoundError, ModuleNotFoundError):
        BoogeyMan()
        export()

Solution (Python):
for s in [*open(0)][1:]:
    print(-all((a := [int(x) % 2 for x in s[:-1]])) or (a[0] + 1) * a[-1])

Solution (Python):
class Test:

    def read_input(self):
        self.N = int(input())
        self.tests = []
        for _ in range(self.N):
            self.tests.append(int(input()))

    def solve(self):
        for i in self.tests:
            if i % 2 == 0:
                print(0)
            elif int(str(i)[0]) % 2 == 0:
                print(1)
            else:
                while True:
                    n = i % 10
                    if n % 2 == 0:
                        print(2)
                        break
                    if i // 10 == 0:
                        print(-1)
                        break
                    i //= 10
if __name__ == '__main__':
    t = Test()
    t.read_input()
    t.solve()