This "Hard" coding problem asks if you can reach a target coordinate starting from using a specific set of moves. You can move from to , , , or . This problem is deceptively simple in its rules but requires deep mathematical insight to solve efficiently.
PhonePe and other fintech companies use this to test a candidate's strength in Number Theory and Math. It’s not a pathfinding problem (BFS would be too slow); it's a Greatest Common Divisor (GCD) problem in disguise. It evaluates whether you can reduce a complex set of rules to a fundamental mathematical property.
The core pattern is Number Theory and GCD. The key observation is that the operations and are exactly what you do in the Euclidean algorithm for finding the GCD. These operations do not change the . The other operations, and , allow you to multiply the coordinates (and thus the GCD) by powers of 2. Therefore, a point is reachable if and only if the is a power of 2.
Target:
The biggest mistake is trying to use Breadth-First Search (BFS) or recursion with memoization. The coordinates can be huge, leading to TLE (Time Limit Exceeded) or memory errors. Another mistake is failing to realize that the "doubling" moves can be thought of as "halving" if you work backwards from the target.
Brush up on the Euclidean algorithm and properties of the Greatest Common Divisor. Many "Hard" grid problems that involve weird movement rules are actually hidden number theory problems. If you see or , think GCD!