PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: pols_agyi_pols
Tester: kingmessi
Editorialist: iceknight1093
DIFFICULTY:
TBD
PREREQUISITES:
None
PROBLEM:
You’re given three integers X, Y, Z. In one move, you can change any one of them to any integer.
Find the minimum number of moves needed to make [X, Y, Z] an arithmetic progression.
EXPLANATION:
If the three values already are in AP, the answer is 0.
As mentioned in the statement, this can be checked by comparing Y-X against Z-Y and seeing if they’re equal.
Otherwise, at least one move is needed.
In fact, we can always use exactly one move!
Since we can change to any integer, all we need to do is change Z to Y + (Y-X) = 2Y-X.
Then, the differences are (Y-X) and (2Y-X) - Y = (Y-X) which are equal, so the numbers are in AP.
TIME COMPLEXITY:
\mathcal{O}(1) per testcase.
CODE:
Editorialist's code (Python)
for _ in range(int(input())):
x, y, z = map(int, input().split())
if y-x == z-y: print(0)
else: print(1)