Reaching Points - Problem

Given four integers sx, sy, tx, and ty, return true if it is possible to convert the point (sx, sy) to the point (tx, ty) through some operations, or false otherwise.

The allowed operation on some point (x, y) is to convert it to either (x, x + y) or (x + y, y).

Input & Output

Example 1 — Reachable Target
$ Input: sx = 1, sy = 1, tx = 3, ty = 5
Output: true
💡 Note: Path exists: (1,1) → (1,2) → (3,2) → (3,5). We can apply operations (1,1+1)=(1,2), then (1+2,2)=(3,2), then (3,3+2)=(3,5).
Example 2 — Unreachable Target
$ Input: sx = 1, sy = 1, tx = 2, ty = 2
Output: false
💡 Note: From (1,1) we can only reach (1,2) or (2,1), but never (2,2). Both coordinates cannot increase simultaneously.
Example 3 — Same Point
$ Input: sx = 1, sy = 1, tx = 1, ty = 1
Output: true
💡 Note: Source and target are the same point, so no operations needed.

Constraints

  • 1 ≤ sx, sy, tx, ty ≤ 109

Visualization

Tap to expand
Reaching Points: Can we get from (1,1) to (3,5)?(1,1)Source(3,5)TargetOperations:(x, y) → (x, x+y)(x, y) → (x+y, y)Path: (1,1) → (1,2) → (3,2) → (3,5)Result: true
Understanding the Visualization
1
Input
Source (sx,sy) and target (tx,ty) coordinates
2
Process
Apply operations (x,x+y) or (x+y,y) to reach target
3
Output
Return true if target is reachable, false otherwise
Key Takeaway
🎯 Key Insight: Working backwards with modulo operations is exponentially faster than forward exploration
Asked in
Google 15 Facebook 8
28.0K Views
Medium Frequency
~25 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen