Watch 6 video solutions for Minimum Moves to Reach Target in Grid, a hard level problem involving Math. This walkthrough by Programming Live with Larry has 637 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given four integers sx, sy, tx, and ty, representing two points (sx, sy) and (tx, ty) on an infinitely large 2D grid.
You start at (sx, sy).
At any point (x, y), define m = max(x, y). You can either:
(x + m, y), or(x, y + m).Return the minimum number of moves required to reach (tx, ty). If it is impossible to reach the target, return -1.
Example 1:
Input: sx = 1, sy = 2, tx = 5, ty = 4
Output: 2
Explanation:
The optimal path is:
max(1, 2) = 2. Increase the y-coordinate by 2, moving from (1, 2) to (1, 2 + 2) = (1, 4).max(1, 4) = 4. Increase the x-coordinate by 4, moving from (1, 4) to (1 + 4, 4) = (5, 4).Thus, the minimum number of moves to reach (5, 4) is 2.
Example 2:
Input: sx = 0, sy = 1, tx = 2, ty = 3
Output: 3
Explanation:
The optimal path is:
max(0, 1) = 1. Increase the x-coordinate by 1, moving from (0, 1) to (0 + 1, 1) = (1, 1).max(1, 1) = 1. Increase the x-coordinate by 1, moving from (1, 1) to (1 + 1, 1) = (2, 1).max(2, 1) = 2. Increase the y-coordinate by 2, moving from (2, 1) to (2, 1 + 2) = (2, 3).Thus, the minimum number of moves to reach (2, 3) is 3.
Example 3:
Input: sx = 1, sy = 1, tx = 2, ty = 2
Output: -1
Explanation:
(2, 2) from (1, 1) using the allowed moves. Thus, the answer is -1.
Constraints:
0 <= sx <= tx <= 1090 <= sy <= ty <= 109Problem Overview: You start from a source cell in a grid and can transform the coordinates using specific additive moves. The goal is to reach a target cell using the minimum number of moves. Direct simulation grows extremely fast, so the challenge is recognizing the mathematical structure behind the transitions.
Approach 1: Forward BFS Simulation (O(Tx * Ty) time, O(Tx * Ty) space)
The most straightforward idea is to treat each coordinate pair as a node in a graph and run a BFS from the starting position. From a cell (x, y), generate the next valid states according to the allowed move rules and push them into a queue. BFS guarantees the first time you reach the target cell it is with the minimum number of moves. The downside is state explosion: coordinates grow quickly and the grid of reachable states can become enormous. This approach is mainly useful for reasoning about the problem or validating small cases.
Approach 2: Bidirectional BFS (O(min(Tx, Ty)^2) time, O(min(Tx, Ty)^2) space)
A common optimization for shortest‑path problems is running BFS from both the start and the target simultaneously. Each side expands one layer at a time until the search frontiers intersect. This significantly reduces the explored state space compared to plain BFS. However, the coordinate growth still makes the search impractical when the target values are large. The approach helps conceptually but still fails for typical competitive programming constraints.
Approach 3: Reverse Mathematical Reduction (O(log(max(Tx, Ty))) time, O(1) space)
The key insight is that the forward moves resemble additive growth such as (x + y, y) or (x, x + y). Instead of building up from the start, work backward from the target. If a coordinate became (x + y, y), the previous state must have been (x, y). Reversing the process repeatedly reduces the larger coordinate by multiples of the smaller one, which is equivalent to the modulo step used in the Euclidean algorithm. Continue applying tx %= ty or ty %= tx depending on which coordinate is larger, counting how many reductions correspond to moves. This greedy reduction quickly shrinks the target coordinates until they match the starting state or prove the path impossible.
This reverse process mirrors the greatest common divisor computation, which is why the complexity becomes logarithmic. The technique relies on mathematical invariants preserved by the allowed moves, making it far more efficient than exploring the grid explicitly. Problems like this often appear under math and number theory, even though the original statement looks like a grid traversal.
Recommended for interviews: Interviewers expect the reverse mathematical reduction. Starting with a BFS explanation shows you understand the graph interpretation, but recognizing the additive structure and converting it into a Euclidean-style reduction demonstrates strong problem‑solving skills. If the discussion involves state exploration or shortest paths, referencing BFS as the naive baseline before presenting the math optimization shows clear progression in reasoning.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Forward BFS Simulation | O(Tx * Ty) | O(Tx * Ty) | Small coordinate limits or when demonstrating shortest path reasoning |
| Bidirectional BFS | O(min(Tx, Ty)^2) | O(min(Tx, Ty)^2) | Moderate constraints where reducing search depth helps |
| Reverse Mathematical Reduction (Optimal) | O(log(max(Tx, Ty))) | O(1) | Large coordinates where exploring the grid is infeasible |