# Solving Leetcode Interviews in Seconds with AI: Check if Point Is Reachable


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "2543" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> 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.   Example 1:  Input: targetX = 6, targetY = 9 Output: false Explanation: It is impossible to reach (6,9) from (1,1) using any sequence of moves, so false is returned.  Example 2:  Input: targetX = 4, targetY = 7 Output: true Explanation: You can follow the path (1,1) -> (1,2) -> (1,4) -> (1,8) -> (1,7) -> (2,7) -> (4,7).    Constraints:  1 <= targetX, targetY <= 109  

	# Explanation
	Here's the solution:

*   **Reverse Engineering:** Instead of trying to reach the target from (1, 1), work backward from (targetX, targetY) towards (1, 1). This is much more efficient because at each step, we can uniquely determine the previous state (or conclude it's impossible).

*   **GCD Connection:** Observe that the allowed operations essentially preserve the greatest common divisor (GCD) of the coordinates. Specifically, gcd(x, y) = gcd(x, x-y) = gcd(x-y,y) = gcd(2x, y) = gcd(x, 2y). Since we start at (1, 1), which has a GCD of 1, any reachable point must also have a GCD of 1.

*   **Euclidean Algorithm:** Use a modified Euclidean algorithm to repeatedly reduce `targetX` and `targetY` until one of them becomes 1. If at any point either `targetX` or `targetY` becomes less than 1, or if after reducing, both are not 1, then the target is unreachable.

*   **Runtime Complexity & Storage Complexity:** O(log(max(targetX, targetY))), O(1)

	
	# Code
	```python
	def reachingPoints(targetX: int, targetY: int) -> bool:
    while targetX > 1 and targetY > 1:
        if targetX > targetY:
            targetX %= targetY
        else:
            targetY %= targetX

    if targetX == 1:
        return (targetY - 1) % targetX == 0
    else:
        return (targetX - 1) % targetY == 0
	```
			
