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.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
You are given three integers x, y, and z, representing the positions of three people on a number line:
x is the position of Person 1.y is the position of Person 2.z is the position of Person 3, who does not move.Both Person 1 and Person 2 move toward Person 3 at the same speed.
Determine which person reaches Person 3 first:
Return the result accordingly.
Example 1:
Input: x = 2, y = 7, z = 4
Output: 1
Explanation:
Since Person 1 reaches Person 3 first, the output is 1.
Example 2:
Input: x = 2, y = 5, z = 6
Output: 2
Explanation:
Since Person 2 reaches Person 3 first, the output is 2.
Example 3:
Input: x = 1, y = 5, z = 3
Output: 0
Explanation:
Since both Person 1 and Person 2 reach Person 3 at the same time, the output is 0.
Constraints:
1 <= x, y, z <= 100Problem summary: You are given three integers x, y, and z, representing the positions of three people on a number line: x is the position of Person 1. y is the position of Person 2. z is the position of Person 3, who does not move. Both Person 1 and Person 2 move toward Person 3 at the same speed. Determine which person reaches Person 3 first: Return 1 if Person 1 arrives first. Return 2 if Person 2 arrives first. Return 0 if both arrive at the same time. Return the result accordingly.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
2 7 4
2 5 6
1 5 3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3516: Find Closest Person
class Solution {
public int findClosest(int x, int y, int z) {
int a = Math.abs(x - z);
int b = Math.abs(y - z);
return a == b ? 0 : (a < b ? 1 : 2);
}
}
// Accepted solution for LeetCode #3516: Find Closest Person
func findClosest(x int, y int, z int) int {
a, b := abs(x-z), abs(y-z)
if a == b {
return 0
}
if a < b {
return 1
}
return 2
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #3516: Find Closest Person
class Solution:
def findClosest(self, x: int, y: int, z: int) -> int:
a = abs(x - z)
b = abs(y - z)
return 0 if a == b else (1 if a < b else 2)
// Accepted solution for LeetCode #3516: Find Closest Person
impl Solution {
pub fn find_closest(x: i32, y: i32, z: i32) -> i32 {
let a = (x - z).abs();
let b = (y - z).abs();
if a == b {
0
} else if a < b {
1
} else {
2
}
}
}
// Accepted solution for LeetCode #3516: Find Closest Person
function findClosest(x: number, y: number, z: number): number {
const a = Math.abs(x - z);
const b = Math.abs(y - z);
return a === b ? 0 : a < b ? 1 : 2;
}
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.