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 an integer n.
Define its mirror distance as: abs(n - reverse(n)) where reverse(n) is the integer formed by reversing the digits of n.
Return an integer denoting the mirror distance of n.
abs(x) denotes the absolute value of x.
Example 1:
Input: n = 25
Output: 27
Explanation:
reverse(25) = 52.abs(25 - 52) = 27.Example 2:
Input: n = 10
Output: 9
Explanation:
reverse(10) = 01 which is 1.abs(10 - 1) = 9.Example 3:
Input: n = 7
Output: 0
Explanation:
reverse(7) = 7.abs(7 - 7) = 0.Constraints:
1 <= n <= 109Problem summary: You are given an integer n. Define its mirror distance as: abs(n - reverse(n)) where reverse(n) is the integer formed by reversing the digits of n. Return an integer denoting the mirror distance of n. abs(x) denotes the absolute value of x.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
25
10
7
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3783: Mirror Distance of an Integer
class Solution {
public int mirrorDistance(int n) {
return Math.abs(n - reverse(n));
}
private int reverse(int x) {
int y = 0;
for (; x > 0; x /= 10) {
y = y * 10 + x % 10;
}
return y;
}
}
// Accepted solution for LeetCode #3783: Mirror Distance of an Integer
func mirrorDistance(n int) int {
reverse := func(x int) int {
y := 0
for ; x > 0; x /= 10 {
y = y*10 + x%10
}
return y
}
return abs(n - reverse(n))
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #3783: Mirror Distance of an Integer
class Solution:
def mirrorDistance(self, n: int) -> int:
return abs(n - int(str(n)[::-1]))
// Accepted solution for LeetCode #3783: Mirror Distance of an Integer
fn mirror_distance(n: i32) -> i32 {
let rev = n.to_string().chars().rev().fold(0, |acc, c| {
acc * 10 + c.to_digit(10).unwrap()
}) as i32;
(n - rev).abs()
}
fn main() {
let ret = mirror_distance(25);
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(mirror_distance(25), 27);
assert_eq!(mirror_distance(10), 9);
assert_eq!(mirror_distance(7), 0);
}
// Accepted solution for LeetCode #3783: Mirror Distance of an Integer
function mirrorDistance(n: number): number {
const reverse = (x: number): number => {
let y = 0;
for (; x > 0; x = Math.floor(x / 10)) {
y = y * 10 + (x % 10);
}
return y;
};
return Math.abs(n - reverse(n));
}
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.