Hey @mikkyimran
Intuition
Every parent point (x, y)
has two children, (x, x+y)
and (x+y, y)
. However, every point (x, y)
only has one parent candidate (x-y, y)
if x >= y
, else (x, y-x)
. This is because we never have points with negative coordinates.
Looking at previous successive parents of the target point, we can find whether the starting point was an ancestor. For example, if the target point is (19, 12)
, the successive parents must have been (7, 12)
, (7, 5)
, and (2, 5)
; so (2, 5)
is a starting point of (19, 12)
.
Algorithm
Repeatedly subtract the smaller of {tx, ty}
from the larger of {tx, ty}
. The answer is true if and only if we eventually reach sx, sy
.