Check if Point Is Reachable - Problem

There exists an infinitely large grid. You are currently at point (1, 1), and you need to reach the point (targetX, targetY) using a finite number of steps.

In one step, you can move from point (x, y) to any one of the following points:

  • (x, y - x)
  • (x - y, y)
  • (2 * x, y)
  • (x, 2 * y)

Given two integers targetX and targetY representing the X-coordinate and Y-coordinate of your final position, return true if you can reach the point from (1, 1) using some number of steps, and false otherwise.

Input & Output

Example 1 — Reachable Point
$ Input: targetX = 6, targetY = 9
Output: false
💡 Note: GCD(6,9) = 3, which is not a power of 2, so point (6,9) cannot be reached from (1,1)
Example 2 — Power of 2 GCD
$ Input: targetX = 4, targetY = 6
Output: true
💡 Note: GCD(4,6) = 2, which is a power of 2, so point (4,6) can be reached from (1,1)
Example 3 — Same Point
$ Input: targetX = 1, targetY = 1
Output: true
💡 Note: We start at (1,1), so it's immediately reachable with 0 steps

Constraints

  • 1 ≤ targetX, targetY ≤ 109

Visualization

Tap to expand
Check if Point Is Reachable: From (1,1) to TargetINPUTtargetX = 6targetY = 9Target PointPROCESSGCD(6, 9) = 33 & 2 = 2 ≠ 0Not Power of 2OUTPUTfalseNot Reachable(1,1)START(6,9)UNREACHABLECannot reach with allowed movesMoves: (x,y-x), (x-y,y), (2x,y), (x,2y)
Understanding the Visualization
1
Input
Target coordinates (targetX, targetY)
2
Process
Calculate GCD and check if it's power of 2
3
Output
Boolean indicating reachability
Key Takeaway
🎯 Key Insight: Use GCD properties - a point is reachable if and only if GCD of coordinates is a power of 2
Asked in
Google 15 Meta 8
8.5K Views
Medium Frequency
~25 min Avg. Time
245 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