PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: utkarsh_25dec
Testers: IceKnight1093, tabr
Editorialist: IceKnight1093
DIFFICULTY:
577
PREREQUISITES:
None
PROBLEM:
Given A and C, does there exist an integer B such that A, B, C are in AP?
EXPLANATION:
For A, B, and C to be in arithmetic progression, there must exist a common difference d such that B-A = C-B = d.
This tells us that 2d = C-B + B-A = C-A, and so d = \frac{C-A}{2}.
So, the only possible value of B is A+d = A + \frac{C-A}{2} = \frac{A+C}{2}.
Under the constraints of the problem, we’d like B to be an integer. Note that \frac{A+C}{2} is an integer if and only if A+C is even. So,
- If A+C is even, the answer is \frac{A+C}{2}
- Otherwise, the answer is -1.
TIME COMPLEXITY:
\mathcal{O}(1) per testcase.
CODE:
Editorialist's code (Python)
for _ in range(int(input())):
a, c = map(int, input().split())
print((a+c)//2 if a%2 == c%2 else -1)