Diagonal Walking v.2 (Codeforces 1036B)
Tags: Math, math

Mikhail walks on a Cartesian plane. He starts at the point $$$(0, 0)$$$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $$$(0, 0)$$$, he can go to any of the following points in one move:   $$$(1, 0)$$$;  $$$(1, 1)$$$;  $$$(0, 1)$$$;  $$$(-1, 1)$$$;  $$$(-1, 0)$$$;  $$$(-1, -1)$$$;  $$$(0, -1)$$$;  $$$(1, -1)$$$. If Mikhail goes from the point $$$(x1, y1)$$$ to the point $$$(x2, y2)$$$ in one move, and $$$x1 \ne x2$$$ and $$$y1 \ne y2$$$, then such a move is called a diagonal move.Mikhail has $$$q$$$ queries. For the $$$i$$$-th query Mikhail's target is to go to the point $$$(n_i, m_i)$$$ from the point $$$(0, 0)$$$ in exactly $$$k_i$$$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $$$(0, 0)$$$ to the point $$$(n_i, m_i)$$$ in $$$k_i$$$ moves.Note that Mikhail can visit any point any number of times (even the destination point!).

Input: The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of queries.Then $$$q$$$ lines follow. The $$$i$$$-th of these $$$q$$$ lines contains three integers $$$n_i$$$, $$$m_i$$$ and $$$k_i$$$ ($$$1 \le n_i, m_i, k_i \le 10^{18}$$$) — $$$x$$$-coordinate of the destination point of the query, $$$y$$$-coordinate of the destination point of the query and the number of moves in the query, correspondingly.

Output: Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to -1 if Mikhail cannot go from the point $$$(0, 0)$$$ to the point $$$(n_i, m_i)$$$ in exactly $$$k_i$$$ moves described above. Otherwise the $$$i$$$-th integer should be equal to the the maximum number of diagonal moves among all possible movements.

Note: One of the possible answers to the first test case: $$$(0, 0) \to (1, 0) \to (1, 1) \to (2, 2)$$$.One of the possible answers to the second test case: $$$(0, 0) \to (0, 1) \to (1, 2) \to (0, 3) \to (1, 4) \to (2, 3) \to (3, 2) \to (4, 3)$$$.In the third test case Mikhail cannot reach the point $$$(10, 1)$$$ in 9 moves.

Sample Inputs:
3
2 2 3
4 3 7
10 1 9

Sample Outputs:
1
6
-1

Solution (Python):
q = []
n = int(input())
for i in range(n):
    (a, b, c) = map(int, input().split())
    g = c % 2
    if abs(a) > c or abs(b) > c:
        q.append(-1)
    elif g == a % 2 and g == b % 2:
        q.append(c)
    elif g != a % 2 and g != b % 2:
        q.append(c - 2)
    else:
        q.append(c - 1)
for i in range(n):
    print(q[i])

Solution (Python):
t = int(input())
mat = []
while t > 0:
    t = t - 1
    L = input().split()
    L = list(map(int, L))
    (x, y, k) = (L[0], L[1], L[2])
    if max(x, y) > k:
        mat.append(-1)
    elif (x + y) % 2:
        mat.append(k - 1)
    elif (x + k) % 2 == 0:
        mat.append(k)
    else:
        mat.append(k - 2)
for a in mat:
    print(a)

Solution (Python):
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys, threading, math, functools
sys.setrecursionlimit(600000)
threading.stack_size(10 ** 8)

def main():
    for _ in range(int(input())):
        [a, b, k] = list(map(int, input().split()))
        if max(a, b) > k:
            print(-1)
            continue
        if (k - max(a, b)) % 2 == 0:
            if abs(b - a) % 2 == 0:
                print(k)
            else:
                print(k - 1)
        elif abs(b - a) % 2 == 0:
            print(k - 2)
        else:
            print(k - 1)
    return
t = threading.Thread(target=main)
t.start()
t.join()