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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Given four integers sx, sy, tx, and ty, return true if it is possible to convert the point (sx, sy) to the point (tx, ty) through some operations, or false otherwise.
The allowed operation on some point (x, y) is to convert it to either (x, x + y) or (x + y, y).
Example 1:
Input: sx = 1, sy = 1, tx = 3, ty = 5 Output: true Explanation: One series of moves that transforms the starting point to the target is: (1, 1) -> (1, 2) (1, 2) -> (3, 2) (3, 2) -> (3, 5)
Example 2:
Input: sx = 1, sy = 1, tx = 2, ty = 2 Output: false
Example 3:
Input: sx = 1, sy = 1, tx = 1, ty = 1 Output: true
Constraints:
1 <= sx, sy, tx, ty <= 109Problem summary: Given four integers sx, sy, tx, and ty, return true if it is possible to convert the point (sx, sy) to the point (tx, ty) through some operations, or false otherwise. The allowed operation on some point (x, y) is to convert it to either (x, x + y) or (x + y, y).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
1 1 3 5
1 1 2 2
1 1 1 1
number-of-ways-to-reach-a-position-after-exactly-k-steps)check-if-point-is-reachable)determine-if-a-cell-is-reachable-at-a-given-time)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #780: Reaching Points
class Solution {
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
while (tx > sx && ty > sy && tx != ty) {
if (tx > ty) {
tx %= ty;
} else {
ty %= tx;
}
}
if (tx == sx && ty == sy) {
return true;
}
if (tx == sx) {
return ty > sy && (ty - sy) % tx == 0;
}
if (ty == sy) {
return tx > sx && (tx - sx) % ty == 0;
}
return false;
}
}
// Accepted solution for LeetCode #780: Reaching Points
func reachingPoints(sx int, sy int, tx int, ty int) bool {
for tx > sx && ty > sy && tx != ty {
if tx > ty {
tx %= ty
} else {
ty %= tx
}
}
if tx == sx && ty == sy {
return true
}
if tx == sx {
return ty > sy && (ty-sy)%tx == 0
}
if ty == sy {
return tx > sx && (tx-sx)%ty == 0
}
return false
}
# Accepted solution for LeetCode #780: Reaching Points
class Solution:
def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:
while tx > sx and ty > sy and tx != ty:
if tx > ty:
tx %= ty
else:
ty %= tx
if tx == sx and ty == sy:
return True
if tx == sx:
return ty > sy and (ty - sy) % tx == 0
if ty == sy:
return tx > sx and (tx - sx) % ty == 0
return False
// Accepted solution for LeetCode #780: Reaching Points
struct Solution;
impl Solution {
fn reaching_points(sx: i32, sy: i32, mut tx: i32, mut ty: i32) -> bool {
while sx < tx && sy < ty {
if tx < ty {
ty %= tx;
} else {
tx %= ty;
}
}
sx == tx && sy <= ty && (ty - sy) % sx == 0 || sy == ty && sx <= tx && (tx - sx) % sy == 0
}
}
#[test]
fn test() {
let sx = 1;
let sy = 1;
let tx = 3;
let ty = 5;
let res = true;
assert_eq!(Solution::reaching_points(sx, sy, tx, ty), res);
let sx = 1;
let sy = 1;
let tx = 2;
let ty = 2;
let res = false;
assert_eq!(Solution::reaching_points(sx, sy, tx, ty), res);
let sx = 1;
let sy = 1;
let tx = 1;
let ty = 1;
let res = true;
assert_eq!(Solution::reaching_points(sx, sy, tx, ty), res);
}
// Accepted solution for LeetCode #780: Reaching Points
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #780: Reaching Points
// class Solution {
// public boolean reachingPoints(int sx, int sy, int tx, int ty) {
// while (tx > sx && ty > sy && tx != ty) {
// if (tx > ty) {
// tx %= ty;
// } else {
// ty %= tx;
// }
// }
// if (tx == sx && ty == sy) {
// return true;
// }
// if (tx == sx) {
// return ty > sy && (ty - sy) % tx == 0;
// }
// if (ty == sy) {
// return tx > sx && (tx - sx) % ty == 0;
// }
// return false;
// }
// }
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.