Mid/SeniorMath

Determine if a Cell Is Reachable at a Given Time

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
Inputsx = 2, sy = 4, fx = 7, fy = 7, t = 6
Outputtrue
Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds.
Example 2
Inputsx = 3, sy = 1, fx = 7, fy = 3, t = 3
Outputfalse
Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3), so it cannot be reached at the third second.

Constraints

  • 1 <= sx, sy, fx, fy <= 10^9
  • 0 <= t <= 10^9

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate