Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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 <= 1090 <= t <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
2 4 7 7 6
3 1 7 3 3
reaching-points)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2849: Determine if a Cell Is Reachable at a Given Time
class Solution {
public boolean isReachableAtTime(int sx, int sy, int fx, int fy, int t) {
if (sx == fx && sy == fy) {
return t != 1;
}
int dx = Math.abs(sx - fx);
int dy = Math.abs(sy - fy);
return Math.max(dx, dy) <= t;
}
}
// Accepted solution for LeetCode #2849: Determine if a Cell Is Reachable at a Given Time
func isReachableAtTime(sx int, sy int, fx int, fy int, t int) bool {
if sx == fx && sy == fy {
return t != 1
}
dx := abs(sx - fx)
dy := abs(sy - fy)
return max(dx, dy) <= t
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #2849: Determine if a Cell Is Reachable at a Given Time
class Solution:
def isReachableAtTime(self, sx: int, sy: int, fx: int, fy: int, t: int) -> bool:
if sx == fx and sy == fy:
return t != 1
dx = abs(sx - fx)
dy = abs(sy - fy)
return max(dx, dy) <= t
// Accepted solution for LeetCode #2849: Determine if a Cell Is Reachable at a Given Time
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2849: Determine if a Cell Is Reachable at a Given Time
// class Solution {
// public boolean isReachableAtTime(int sx, int sy, int fx, int fy, int t) {
// if (sx == fx && sy == fy) {
// return t != 1;
// }
// int dx = Math.abs(sx - fx);
// int dy = Math.abs(sy - fy);
// return Math.max(dx, dy) <= t;
// }
// }
// Accepted solution for LeetCode #2849: Determine if a Cell Is Reachable at a Given Time
function isReachableAtTime(sx: number, sy: number, fx: number, fy: number, t: number): boolean {
if (sx === fx && sy === fy) {
return t !== 1;
}
const dx = Math.abs(sx - fx);
const dy = Math.abs(sy - fy);
return Math.max(dx, dy) <= t;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.