← Back to DSA
#3783

Mirror Distance of an Integer

Mirror Distance of an Integer solution for LeetCode 3783, with the key idea, complexity breakdown, and working code in Java, C++, JavaScript, TypeScript, C, Go, and Rust.

Easy
Math
Solve on LeetCode ↗

Mirror Distance of an Integer

Problem

We are given an integer n.

The mirror distance is defined as:

abs(n - reverse(n))

Here, reverse(n) means the integer formed by reversing the digits of n.

For example:

  • reverse(25) = 52
  • reverse(10) = 1, because leading zeros are not kept in integers
  • reverse(7) = 7

We need to return the absolute difference between the original number and its reversed form.

Intuition

The core task is simply reversing the digits of a number.

To reverse a number mathematically, repeatedly take its last digit and append it to a new number.

For example, for n = 250:

reversed = 0

take 0 -> reversed = 0
take 5 -> reversed = 5
take 2 -> reversed = 52

So reverse(250) = 52.

After that, the answer is just:

abs(original - reversed)

The only small detail is that we must store the original value before modifying n, because the digit reversal loop keeps dividing n by 10.

Approach #1: Digit Reversal

We can reverse the number using modulo and division.

At each step:

  • n % 10 gives the last digit
  • reversed * 10 + digit appends that digit to the reversed number
  • n / 10 removes the last digit

Once all digits are processed, compute the absolute difference.

Algorithm

  1. Store the original value of n.
  2. Initialize reversed = 0.
  3. While n > 0:
    • get the last digit using n % 10
    • append it to reversed
    • remove the last digit from n
  4. Return abs(original - reversed).

Code Solution

Switch between languages

class Solution {
    public int mirrorDistance(int n) {
        int original = n;
        int reversed = 0;

        while (n > 0) {
            reversed = reversed * 10 + n % 10;
            n /= 10;
        }

        return Math.abs(original - reversed);
    }
}

Dry Run

Take n = 25.

original = 25
reversed = 0

First iteration:

digit = 25 % 10 = 5
reversed = 0 * 10 + 5 = 5
n = 25 / 10 = 2

Second iteration:

digit = 2 % 10 = 2
reversed = 5 * 10 + 2 = 52
n = 2 / 10 = 0

Now:

abs(25 - 52) = 27

So the answer is 27.

Handling trailing zeros

Take n = 10.

The reverse process gives:

10 -> 1

The reversed digits would look like 01, but as an integer this is simply 1.

So:

abs(10 - 1) = 9

This works naturally with the mathematical approach because integers do not store leading zeros.

Why This Works

Every loop iteration moves exactly one digit from the end of n to the end of reversed.

If the original digits are:

d1 d2 d3 ... dk

then the loop appends them to reversed in this order:

dk ... d3 d2 d1

That is exactly the definition of reversing the digits.

After we have the reversed number, taking the absolute difference gives the mirror distance requested by the problem.

Complexity Analysis

Let d be the number of digits in n.

Time Complexity: O(d), which is also O(log n)

We process each digit once.

Space Complexity: O(1)

We only use a few integer variables.

Common Mistakes

1. Losing the original number

The loop changes n, so keep a separate original variable before starting the reversal.

2. Treating 01 as a special case

No special handling is needed. When reversing 10, the result is the integer 1.

3. Forgetting absolute value

For n = 25, 25 - 52 = -27, but the mirror distance is 27.

Always return the absolute difference.

Final Takeaway

This is a direct number manipulation problem.

Whenever we need to reverse digits, the standard pattern is:

reversed = reversed * 10 + n % 10
n = n / 10

Once the reversed number is ready, the mirror distance is just the absolute difference from the original number.

Dynamic Programming

7 DP Patterns > 100 LeetCode Questions

Most DP questions are repeated ideas. Stop treating DP like chaos. Learn the 7 repeatable patterns that unlock most placement-level questions.

7 patternsProgress tracking
Read 7 patterns (5 min)