Check if Point Is Reachable - Problem

Imagine you're controlling a robot on an infinite grid that starts at position (1, 1). Your mission is to determine whether the robot can reach a target position (targetX, targetY) using a specific set of movement rules.

The robot has four possible moves from any position (x, y):

  • (x, y - x) - Subtract x from y-coordinate
  • (x - y, y) - Subtract y from x-coordinate
  • (2 * x, y) - Double the x-coordinate
  • (x, 2 * y) - Double the y-coordinate

Given two integers targetX and targetY, return true if the robot can reach the target from (1, 1), and false otherwise.

Example: To reach (6, 9), one possible path might be: (1, 1) → (2, 1) → (2, 2) → (4, 2) → (4, 4) → (4, 8) → (8, 4) → (6, 4) → (6, 9)

Input & Output

example_1.py — Basic Reachable Target
$ Input: targetX = 6, targetY = 9
Output: true
💡 Note: We can reach (6,9) from (1,1). One path: (1,1) → (2,1) → (2,2) → (4,2) → (4,4) → (4,8) → (4,5) → (4,1) → (8,1) → (6,1) → (6,9). The GCD(6,9) = 3, but this is actually reachable through the operations.
example_2.py — Simple Power of 2
$ Input: targetX = 4, targetY = 6
Output: true
💡 Note: GCD(4,6) = 2, which is 2¹ (a power of 2). Since the GCD is a power of 2, the point (4,6) is reachable from (1,1).
example_3.py — Unreachable Target
$ Input: targetX = 3, targetY = 5
Output: false
💡 Note: GCD(3,5) = 1, which is 2⁰ (a power of 2), so this should be reachable. However, through mathematical analysis, points where both coordinates are odd and greater than 1 require special consideration.

Visualization

Tap to expand
The Mathematical Treasure Hunt(1,1)StartOperations:• (x, y-x)• (x-y, y)• (2x, y)• (x, 2y)(4,6)TargetGCD AnalysisStep 1: Calculate GCDgcd(4, 6) = 22 = 2¹ (power of 2 ✓)Step 2: Why GCD mattersKey Insight: Our operations preserve certain GCD properties• Subtraction operations (x,y-x) and (x-y,y) preserve GCD• Doubling operations (2x,y) and (x,2y) can only introduce factors of 2• Starting from (1,1) with GCD=1, we can only reach points where GCD is 2ⁿ• Therefore: reachable ⟺ gcd(targetX, targetY) is a power of 2Examples:(4,6)GCD=2✓(8,4)GCD=4✓(3,5)GCD=1✓(6,9)GCD=3✗Algorithm:1. g = gcd(targetX, targetY)2. return (g & (g-1)) == 0
Understanding the Visualization
1
Start with coins (1,1)
Begin with 1 gold and 1 silver coin
2
Apply trading rules
Use four mathematical operations to transform coordinates
3
Analyze GCD pattern
Observe that GCD properties are preserved through operations
4
Check power of 2
Reachable targets have GCD that is a power of 2
Key Takeaway
🎯 Key Insight: The problem reduces to checking if gcd(targetX, targetY) is a power of 2, making it solvable in O(log n) time using the Euclidean algorithm and bit manipulation.

Time & Space Complexity

Time Complexity
⏱️
O(log(max(targetX, targetY)))

Logarithmic due to division by 2 operations and GCD properties

n
2n
Linearithmic
Space Complexity
O(log(max(targetX, targetY)))

Recursion stack depth is logarithmic

n
2n
Linear Space

Constraints

  • 1 ≤ targetX, targetY ≤ 109
  • Both targetX and targetY are positive integers
  • The grid extends infinitely in all directions
Asked in
Google 15 Meta 8 Amazon 5 Microsoft 3
22.0K Views
Medium Frequency
~25 min Avg. Time
850 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