Check if Point Is Reachable - Complete Solution Guide
Check if Point Is Reachable is LeetCode problem 2543, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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 . Exampl
Detailed Explanation
The problem asks whether a point (targetX, targetY) is reachable from (1, 1) on an infinite grid. The allowed moves are (x, y) -> (x, y - x), (x, y) -> (x - y, y), (x, y) -> (2 * x, y), and (x, y) -> (x, 2 * y). We need to return `true` if the target is reachable, and `false` otherwise. The constraints are 1 <= targetX, targetY <= 10^9.
Solution Approach
The solution leverages the properties of the GCD under the allowed moves. The core idea is that if a point (targetX, targetY) is reachable from (1, 1), then their greatest common divisor (GCD) must be a power of 2. This is because the initial GCD is 1, and each operation either preserves the GCD or multiplies it by 2. The solution calculates the GCD of targetX and targetY and then checks if the GCD is a power of 2 using a bitwise operation (g & (g - 1)) == 0. If it is, the target is reachable; otherwise, it is not.
Step-by-Step Algorithm
- Step 1: Calculate the greatest common divisor (GCD) of targetX and targetY using the Euclidean algorithm.
- Step 2: Check if the calculated GCD is a power of 2. This is done efficiently by checking if (GCD & (GCD - 1)) == 0. If the result is 0, then the GCD is a power of 2.
- Step 3: Return true if the GCD is a power of 2; otherwise, return false.
Key Insights
- Insight 1: The greatest common divisor (GCD) plays a crucial role in determining reachability. Operations (x, y-x) and (x-y, y) preserve the GCD. Operations (2x, y) and (x, 2y) can at most multiply the GCD by 2.
- Insight 2: Starting from (1, 1), the GCD is 1. Any reachable point (targetX, targetY) must have a GCD that is a power of 2.
- Insight 3: The reverse operations (x, y+x), (x+y, y), (x/2, y), and (x, y/2) are also important for understanding the problem. The greatest odd divisor of the GCD is an invariant when applying these reverse operations. Since gcd_odd(1, 1) is 1, any reachable point must also have a gcd with greatest odd divisor of 1, implying the GCD is a power of 2.
- Insight 4: A number is a power of 2 if and only if (n & (n - 1)) == 0, making it an efficient way to check the result.
Complexity Analysis
Time Complexity: O(log(min(x, y)))
Space Complexity: O(1)
Topics
This problem involves: Math, Number Theory.
Companies
Asked at: PhonePe.