LeetCode 3783 Mirror Distance of an Integer | Java Solution Explained
LeetCode 3783 Mirror Distance of an Integer | Java Solution Explained

LeetCode 3783 Mirror Distance of an Integer | Java Solution Explained

Learn how to solve LeetCode 3783 using digit reversal and math. Includes Java code, examples, and step-by-step explanation.

16 views
0
0
Listen to articleAudio version

Introduction

Some problems test complex algorithms, while others focus on fundamental concepts done right.

LeetCode 3783 – Mirror Distance of an Integer falls into the second category.

This problem is simple yet important because it builds understanding of:

  1. Digit manipulation
  2. Reversing numbers
  3. Mathematical operations

In this article, we’ll break down the problem in a clean and intuitive way, along with an optimized Java solution.

πŸ”— Problem Link

LeetCode: Mirror Distance of an Integer

Problem Statement

You are given an integer n.

The mirror distance is defined as:

| n - reverse(n) |

Where:

  1. reverse(n) = number formed by reversing digits of n
  2. |x| = absolute value

πŸ‘‰ Return the mirror distance.

Examples

Example 1

Input:

n = 25

Output:

27

Explanation:

reverse(25) = 52

|25 - 52| = 27

Example 2

Input:

n = 10

Output:

9

Explanation:

reverse(10) = 1

|10 - 1| = 9

Example 3

Input:

n = 7

Output:

0

Key Insight

The problem consists of two simple steps:

1. Reverse the number
2. Take absolute difference

Intuition

Let’s take an example:

n = 120

Step 1: Reverse digits

120 β†’ 021 β†’ 21

πŸ‘‰ Leading zeros are ignored automatically.

Step 2: Compute difference

|120 - 21| = 99

Approach

Step-by-Step

  1. Extract digits using % 10
  2. Build reversed number
  3. Use Math.abs() for final result

Java Code

class Solution {

// Function to reverse a number
public int reverse(int k) {

int rev = 0;

while (k != 0) {

int dig = k % 10; // get last digit
k = k / 10; // remove last digit

rev = rev * 10 + dig; // build reversed number
}

return rev;
}

public int mirrorDistance(int n) {

// Calculate mirror distance
return Math.abs(n - reverse(n));
}
}

Dry Run

Input:

n = 25

Execution:

  1. Reverse β†’ 52
  2. Difference β†’ |25 - 52| = 27

Complexity Analysis

Time Complexity

  1. Reversing number β†’ O(d)
  2. (d = number of digits)

πŸ‘‰ Overall: O(log n)

Space Complexity

πŸ‘‰ O(1) (no extra space used)

Why This Works

  1. Digit extraction ensures correct reversal
  2. Leading zeros automatically removed
  3. Absolute difference ensures positive result

Edge Cases to Consider

  1. Single digit β†’ result = 0
  2. Numbers ending with zero (e.g., 10 β†’ 1)
  3. Large numbers (up to 10⁹)

Key Takeaways

  1. Simple math problems can test core logic
  2. Digit manipulation is a must-know skill
  3. Always handle leading zeros carefully
  4. Use built-in functions like Math.abs() effectively

Real-World Relevance

Concepts used here are helpful in:

  1. Number transformations
  2. Palindrome problems
  3. Reverse integer problems
  4. Mathematical algorithms

Conclusion

The Mirror Distance of an Integer problem is a great example of combining basic operations to form a meaningful solution.

While simple, it reinforces important programming fundamentals that are widely used in more complex problems.

Frequently Asked Questions (FAQs)

1. What happens to leading zeros in reverse?

They are automatically removed when stored as an integer.

2. Can this be solved using strings?

Yes, but integer-based approach is more efficient.

3. What is the best approach?

Using arithmetic operations (% and /) is optimal.

Ai Assistant Kas