Is Possible Path (Grid Reachability)

Problem On an infinite grid you start at cell (a, b) and want to reach (c, d). From (x, y) you may move to (x+y, y) or (x, x+y). Determine whether (c, d) is reachable from (a, b).

Input / Output

  • Input: four positive integers a, b, c, d.
  • Output: boolean — whether (c, d) is reachable.

Constraints

  • Values up to 10^9, so a forward BFS explodes; an O(log max(c,d)) solution is expected.
  • All coordinates are positive integers.

Example

  • (1,1) → (5,3): reachable.
  • (1,1) → (2,2): not reachable — from (1,1) the only moves are to (2,1) or (1,2), and no sequence produces two equal coordinates greater than 1.
asked …
LeaderboardSalaryAccount