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
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
⚡ Linearithmic
Space Complexity
O(log(max(targetX, targetY)))
Recursion stack depth is logarithmic
✓ Linear Space
Constraints
- 1 ≤ targetX, targetY ≤ 109
- Both targetX and targetY are positive integers
- The grid extends infinitely in all directions
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code