Skip to main content

Determine if a Cell Is Reachable at a Given Time - Solution & Explanation

MediumMath18 min readAsked at: Google, GitHub
Practice this problem

Problem Statement

You are given four integers sx, sy, fx, fy, and a non-negative integer t.

In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells.

Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise.

A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.

 

Example 1:

Input: sx = 2, sy = 4, fx = 7, fy = 7, t = 6
Output: true
Explanation: Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above. 

Example 2:

Input: sx = 3, sy = 1, fx = 7, fy = 3, t = 3
Output: false
Explanation: Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second.

 

Constraints:

  • 1 <= sx, sy, fx, fy <= 109
  • 0 <= t <= 109

Approach Overview

Problem Overview: You start at cell (sx, sy) on an infinite grid and want to reach (fx, fy) exactly at time t. In one second you may move to any of the 8 neighboring cells (horizontal, vertical, or diagonal). The task is to determine whether the destination can be reached in exactly t moves.

Approach 1: Minimum Steps with Parity Check (O(1) time, O(1) space)

The key observation is that diagonal movement allows you to reduce both row and column distance at the same time. The minimum number of moves needed is the Chebyshev distance: max(|sx - fx|, |sy - fy|). If this minimum distance is greater than t, reaching the target is impossible. If the distance is less than or equal to t, you can usually spend extra moves by wandering around nearby cells.

There is one edge case: when the start and end cells are the same and t = 1. Any move must change the position, so returning to the same cell in exactly one step is impossible. For all other cases where t is at least the minimum distance, the destination is reachable. This constant‑time observation makes the problem a pure math reasoning exercise rather than a full search.

Approach 2: Simulation with BFS Algorithm (O(t²) time, O(t²) space)

A straightforward approach treats the grid as a graph where each cell connects to its eight neighbors. Starting from (sx, sy), perform a Breadth‑First Search (BFS) and expand all reachable cells level by level until time t. If the destination appears at exactly level t, return true.

Because the grid is infinite, BFS must be limited to cells that can possibly be reached within t moves, effectively a square region around the start. This leads to roughly O(t²) states in the worst case. While this approach models the movement rules directly and is easy to reason about, it is far slower than the mathematical observation.

Recommended for interviews: Interviewers expect the constant‑time math solution using Chebyshev distance. It shows you recognize the movement pattern of 8-direction grids and avoid unnecessary graph traversal. Discussing the BFS simulation first can demonstrate baseline reasoning, but the O(1) approach proves stronger algorithmic insight.

Approach 1: Approach 1: Minimum Steps with Parity Check

This approach leverages the concept of "Manhattan distance" coupled with the "parity check" to determine if the cell can be reached in exactly t steps.

The minimum number of steps required to reach from (sx, sy) to (fx, fy) in a grid is the maximum of the horizontal and vertical distances between these points, known as Chebyshev distance. If this minimal distance is greater than t, the function should return false instantly.

Moreover, if the difference between t and this minimum distance is even, the extra steps required can effectively be taken by oscillating on the grid or taking additional allowable steps. If not, reaching in t steps becomes impossible.

The function calculates the minimum steps needed using the max function for the absolute horizontal and vertical distances. It then checks if these steps are less than or equal to t and if the difference is even to return true or false.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), as it involves simple arithmetic operations.

Space Complexity: O(1), as no extra space is used.

Try this approach in the editor →

Approach 2: Approach 2: Simulation with BFS Algorithm

This approach models the grid and implements a Breadth-First Search (BFS) to simulate traversing from the starting point to check if reaching precisely at t seconds is possible.

Instead of directly computing the distance, this method carries out a brute-force simulation of the movement, exploring all potential paths using a FIFO queue structure inherent to BFS algorithms. The goal is to maintain a count of steps and analyze pathways that may uniquely cover the grid in t steps.

This approach is conceptual as implementing full BFS is impractical for infinite grids or large bounds directly within C without optimized constraints.

The BFS is understood as a simulation iterating every possible step sequence up to t.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: Highly complex, typically O(n^2).

Space Complexity: Also complex, with decisions based on array allocations with a theoretical upper limit.

Try this approach in the editor →

Approach 3: Case Discussion

If the starting point and the destination are the same, then we can only reach the destination within the given time if t neq 1.

Otherwise, we can calculate the difference in the x and y coordinates between the starting point and the destination, and then take the maximum value. If the maximum value is less than or equal to the given time, then we can reach the destination within the given time.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Minimum Steps with Parity Check

Time Complexity: O(1), as it involves simple arithmetic operations.

Space Complexity: O(1), as no extra space is used.

Approach 2: Simulation with BFS Algorithm

Time Complexity: Highly complex, typically O(n^2).

Space Complexity: Also complex, with decisions based on array allocations with a theoretical upper limit.

Case Discussion

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Minimum Steps with Parity Check (Math)O(1)O(1)Best approach. Uses Chebyshev distance to compute the minimum moves directly.
Simulation with BFSO(t²)O(t²)Useful for understanding grid movement or when deriving the mathematical shortcut.

Video Solution

Determine if a Cell Is Reachable at a Given Time | Intuition | Math | Leetcode - 2849codestorywithMIK3,319 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Determine if a Cell Is Reachable at a Given Time easy or hard?
The problem is labeled Medium because the trick is recognizing Chebyshev distance and the special edge case. Once the observation is made, the implementation becomes a constant‑time check with only a few arithmetic operations.
Determine if a Cell Is Reachable at a Given Time Python/Java solution
Most implementations compute the Chebyshev distance using simple arithmetic. Python, Java, C++, and JavaScript versions typically calculate dx = abs(sx - fx) and dy = abs(sy - fy), check max(dx, dy) <= t, and handle the edge case where sx == fx, sy == fy, and t == 1.
How to solve Determine if a Cell Is Reachable at a Given Time in O(1)?
Calculate the Chebyshev distance between the start and finish cells: max(|sx - fx|, |sy - fy|). If this minimum distance is greater than t, the destination cannot be reached. If it is less than or equal to t, the destination is reachable except when the start and finish are the same cell and t equals 1, because you must move but cannot return in one step.
What is the best approach for Determine if a Cell Is Reachable at a Given Time?
The optimal approach uses Chebyshev distance. Compute max(|sx - fx|, |sy - fy|) to get the minimum number of moves required when diagonal movement is allowed. If this value is greater than t, reaching the cell is impossible; otherwise it is reachable except for the special case where the start equals the end and t equals 1. This solution runs in O(1) time and O(1) space.
Is Determine if a Cell Is Reachable at a Given Time asked at Google/Amazon/Meta?
Grid movement and distance‑calculation problems appear frequently in interviews at companies like Amazon, Google, and Meta. Variants often test recognition of Manhattan or Chebyshev distance and the ability to replace brute‑force graph traversal with a mathematical shortcut.
What data structure is used in Determine if a Cell Is Reachable at a Given Time?
The optimal solution does not require any data structure because it relies on a mathematical distance formula. A brute‑force simulation version may use a queue for Breadth‑First Search along with a visited set to track explored grid cells.
What is the time complexity of Determine if a Cell Is Reachable at a Given Time?
The optimal mathematical solution runs in O(1) time and O(1) space because it only computes absolute differences and a maximum operation. A simulation approach using BFS explores reachable grid cells and can take O(t²) time and space since the search area grows with the number of steps.

Ready to solve this problem?

Practice Determine if a Cell Is Reachable at a Given Time with our built-in code editor and test cases.

Practice on FleetCode