Skip to main content

Minimum Moves to Reach Target in Grid - Solution & Explanation

HardMath4 min readAsked at: Bloomberg
Practice this problem

Problem Statement

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:

  • Move to (x + m, y), or
  • Move to (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:

  • Move 1: max(1, 2) = 2. Increase the y-coordinate by 2, moving from (1, 2) to (1, 2 + 2) = (1, 4).
  • Move 2: 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:

  • Move 1: max(0, 1) = 1. Increase the x-coordinate by 1, moving from (0, 1) to (0 + 1, 1) = (1, 1).
  • Move 2: max(1, 1) = 1. Increase the x-coordinate by 1, moving from (1, 1) to (1 + 1, 1) = (2, 1).
  • Move 3: 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:

  • It is impossible to reach (2, 2) from (1, 1) using the allowed moves. Thus, the answer is -1.

 

Constraints:

  • 0 <= sx <= tx <= 109
  • 0 <= sy <= ty <= 109

Approach Overview

Problem 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.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Forward BFS SimulationO(Tx * Ty)O(Tx * Ty)Small coordinate limits or when demonstrating shortest path reasoning
Bidirectional BFSO(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

Video Solution

3609. Minimum Moves to Reach Target in Grid (Leetcode Hard) • Programming Live with Larry • 647 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Minimum Moves to Reach Target in Grid easy or hard?
The problem is classified as Hard because the brute-force BFS idea quickly fails for large inputs. The intended solution requires recognizing the mathematical pattern and converting the process into a reverse Euclidean reduction with logarithmic complexity.
Minimum Moves to Reach Target in Grid Python/Java solution
Most implementations follow the reverse reduction strategy. The algorithm repeatedly applies modulo operations to shrink the larger coordinate until it either reaches the start cell or becomes invalid. The logic is identical across Python, Java, C++, and Go, with only syntax differences.
How to solve Minimum Moves to Reach Target in Grid in O(log n)?
Work backward from the target coordinates instead of expanding from the start. If the allowed moves are additive, the previous state must be derived by subtracting the smaller coordinate from the larger one. Using modulo operations (tx %= ty or ty %= tx) compresses multiple reverse steps into one, producing an O(log n) solution.
What is the best approach for Minimum Moves to Reach Target in Grid?
The most efficient solution works backward from the target using a mathematical reduction similar to the Euclidean algorithm. Instead of simulating moves forward, repeatedly reduce the larger coordinate using modulo operations until it matches the start state. This approach runs in O(log(max(tx, ty))) time and uses constant space.
Is Minimum Moves to Reach Target in Grid asked at Google/Amazon/Meta?
Grid reachability and reverse-math problems appear frequently in interviews at companies like Google, Amazon, and Meta. Variants of additive coordinate transformations and Euclidean-style reductions are commonly used to test mathematical reasoning and optimization skills.
What data structure is used in Minimum Moves to Reach Target in Grid?
The brute-force interpretation uses a queue for BFS because the grid can be viewed as a graph of coordinate states. The optimal solution does not require complex data structures and instead relies on arithmetic operations and mathematical invariants.
What is the time complexity of Minimum Moves to Reach Target in Grid?
The optimal solution runs in O(log(max(tx, ty))) time because each step reduces one coordinate using a modulo operation, similar to the GCD algorithm. Space complexity is O(1) since only a few variables are tracked during the reverse reduction process.

Ready to solve this problem?

Practice Minimum Moves to Reach Target in Grid with our built-in code editor and test cases.

Practice on FleetCode