SHARING - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: raysh07
Tester: iceknight1093
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

Alice has A cookies, Bob has B, where A \gt B.
Is it possible for Alice to give some cookies to Bob so that they have an equal number of cookies?
If yes, how many should she give him?

EXPLANATION:

The total number of cookies between Alice and Bob is A+B.
If they must have an equal number of cookies, then each person must have

\frac{A+B}{2}

cookies.

If A+B is an odd number, then \frac{A+B}{2} is not an integer, so such a distribution is impossible.
So, the answer is -1 in this case.

If A+B is an even number, it’s possible for a valid distribution to exist.
Alice has A cookies, and wants to end up with \frac{A+B}{2}.
This means she must give away

A - \frac{A+B}{2} = \frac{2A-A-B}{2} = \frac{A-B}{2}

cookies, which is the answer.

TIME COMPLEXITY:

\mathcal{O}(1) per testcase.

CODE:

Editorialist's code (PyPy3)
a, b = map(int, input().split())
if (a+b)%2 == 1: print(-1)
else: print((a-b)//2)