Skip to content

Commit 3a4fa12

Browse files
authored
Merge pull request #980 from vivekvardhan2810/main
Check if Point is Reachable (Leetcode) Added problem number 233
2 parents 41c9a24 + 3e0f0e3 commit 3a4fa12

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
id: check-if-point-is-reachable
3+
title: Check if Point is Reachable
4+
sidebar_label: 2543 Check if Point is Reachable
5+
tags:
6+
- Number Theory
7+
- Java
8+
- gcd
9+
- Math
10+
description: "This document provides a solution where we need to reach the point (targetX, targetY) using a finite number of steps."
11+
---
12+
13+
## Problem
14+
15+
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.
16+
17+
In one Step, you can move from point $(x, y)$ to any one of the following points:
18+
19+
- $(x, y - x)$
20+
21+
- $(x - y, y)$
22+
23+
- $(2 * x, y)$
24+
25+
- $(x, 2 * y)$
26+
27+
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.
28+
29+
### Examples
30+
31+
**Example 1:**
32+
33+
```
34+
Input: targetX = 6, targetY = 9
35+
36+
Output: false
37+
38+
Explanation: It is impossible to reach (6,9) from (1,1) using any sequence of moves, so false is returned.
39+
40+
```
41+
42+
**Example 2:**
43+
44+
```
45+
Input: targetX = 4, targetY = 7
46+
47+
Output: true
48+
49+
Explanation: You can follow the path (1,1) -> (1,2) -> (1,4) -> (1,8) -> (1,7) -> (2,7) -> (4,7).
50+
51+
```
52+
53+
### Constraints
54+
55+
- `1 <= targetX, targetY <= 10^9`
56+
57+
---
58+
59+
## Approach
60+
61+
To solve the problem, we need to understand the nature of the allowed moves:
62+
63+
1. $(x, y - x)$
64+
65+
2. $(x - y, y)$
66+
67+
3. $(2 * x, y)$
68+
69+
4. $(x, 2 * y)$
70+
71+
These moves suggest that if we can reach a certain point **'(x, y)'**, we can generate other points by specific transformations. By analyzing the moves:
72+
73+
- The first two moves involve subtraction, reducing one coordinate.
74+
75+
- The last two moves involve multiplication by $2$.
76+
77+
We can reverse the logic to check whether we can reach $(1, 1)$ from $(targetX, targetY)$. This reversal involves:
78+
79+
- Checking if $targetX$ or $targetY$ can be divided by $2$ (to reverse the multiplication).
80+
81+
- Checking if one coordinate can be reduced by subtracting the other.
82+
83+
By reversing the operations, we trace the problem back to whether $(targetX, targetY)$ can be reduced to $(1, 1)$.
84+
85+
The key insight here is that we only need to check if **'gcd(targetX, targetY)'** is a power of $2$. This is because if both $targetX$ and $targetY$ share a common factor other than $2$, it won't be possible to reach $(1, 1)$.
86+
87+
88+
## Solution for Check if Point is Reachable
89+
90+
The given problem involves reaching a target point $(targetX, targetY)$ from the starting point $(1, 1)$ using a set of allowed moves. The challenge is to determine whether it's possible to reach the target using these moves.
91+
92+
#### Code in Java
93+
94+
```java
95+
class Solution {
96+
public boolean isReachable(int targetX, int targetY) {
97+
return Integer.bitCount(gcd(targetX, targetY)) == 1;
98+
}
99+
100+
private int gcd(int a, int b) {
101+
return b == 0 ? a : gcd(b, a % b);
102+
}
103+
}
104+
105+
```
106+
107+
### Complexity Analysis
108+
109+
#### Time Complexity: $O(log(min(targetX, targetY)))$
110+
111+
> **Reason**: Calculating the gcd of two numbers takes $O(log(min(targetX, targetY)))$ time using the Euclidean algorithm.
112+
113+
#### Space Complexity: $O(1)$
114+
115+
> **Reason**: The space complexity is $O(1)$ since we are using a constant amount of extra space for computation (ignoring the space used by the recursive stack of the gcd function, which is logarithmic in terms of the depth of the recursion).
116+
117+
# References
118+
119+
- **LeetCode Problem:** [Check if Point is Reachable](https://leetcode.com/problems/check-if-point-is-reachable/description/)
120+
- **Solution Link:** [Check if Point is Reachable Solution on LeetCode](https://leetcode.com/problems/check-if-point-is-reachable/solutions/)
121+
- **Authors LeetCode Profile:** [Vivek Vardhan](https://leetcode.com/u/vivekvardhan43862/)

0 commit comments

Comments
 (0)