Skip to main content

Mirror Distance of an Integer - Solution & Explanation

EasyMath6 min readAsked at: Google, Bloomberg
Practice this problem

Problem Statement

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.
  • Thus, the answer is abs(25 - 52) = 27.

Example 2:

Input: n = 10

Output: 9

Explanation:

  • reverse(10) = 01 which is 1.
  • Thus, the answer is abs(10 - 1) = 9.

Example 3:

Input: n = 7

Output: 0

Explanation:

  • reverse(7) = 7.
  • Thus, the answer is abs(7 - 7) = 0.

 

Constraints:

  • 1 <= n <= 109

Approach Overview

Problem Overview: You are given an integer n. Create its mirror by reversing the decimal digits, then return the absolute difference between the original number and this mirrored value. The task is essentially digit manipulation: construct the reversed integer and compute |n - mirror(n)|.

Approach 1: String-Based Simulation (O(d) time, O(d) space)

Convert the integer to a string and reverse the character sequence. Parse the reversed string back into an integer to obtain the mirrored value. The mirror distance is simply abs(n - mirrored). This approach is straightforward and easy to implement because built-in string operations handle the reversal. Time complexity is O(d) where d is the number of digits, and space complexity is also O(d) due to the temporary string representation.

Approach 2: Mathematical Digit Reversal (O(d) time, O(1) space)

A more space-efficient method reverses the digits using arithmetic. Repeatedly extract the last digit with n % 10, append it to the mirrored number using mirror = mirror * 10 + digit, and remove the digit from the original value with integer division. Once all digits are processed, compute the distance using abs(original - mirror). This approach performs a simple digit-by-digit simulation and avoids extra memory, making the space complexity O(1) while maintaining O(d) time.

Both implementations rely on basic digit manipulation, a common pattern in math and simulation problems. Understanding how to extract and rebuild digits using modulo and division is useful for many related problems involving number reversal or palindromes.

Recommended for interviews: The mathematical digit-reversal approach is usually preferred. Interviewers expect you to handle integer digit operations without converting to strings. Showing the string-based version first demonstrates clarity, but implementing the O(1) space arithmetic solution shows stronger command of math fundamentals and low-level number manipulation.

Solution

We define a function reverse(x) to reverse the digits of integer x. Specifically, we initialize a variable y to 0, then repeatedly append the last digit of x to the end of y, and remove the last digit from x, until x becomes 0. Finally, y is the reversed integer.

Next, we compute the mirror distance of integer n, which is abs(n - reverse(n)), and return the result.

The time complexity is O(log n) and the space complexity is O(1), where n is the size of the input integer.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String-Based ReversalO(d)O(d)Quick implementation when clarity matters more than memory
Mathematical Digit ReversalO(d)O(1)Preferred in interviews or memory-constrained environments

Video Solution

Mirror Distance of an Integer | 2 Ways | Simple Explanation | Leetcode 3783 | codestorywithMIK • codestorywithMIK • 1,752 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Mirror Distance of an Integer easy or hard?
Mirror Distance of an Integer is classified as an Easy problem. It mainly tests basic digit manipulation, integer reversal logic, and simple arithmetic operations rather than advanced algorithms or data structures.
Mirror Distance of an Integer Python/Java solution
In Python or Java, iterate through digits to build the reversed number using modulo and division. Maintain a variable for the mirrored value, update it as mirror = mirror * 10 + digit, and finally return Math.abs(original - mirror) or abs(original - mirror).
How to solve Mirror Distance of an Integer in O(n)?
Treat n as the number of digits. Reverse the digits either using string reversal or arithmetic operations. After constructing the mirrored value, return abs(original - mirrored). Since each digit is processed once, the algorithm runs in O(d) time with constant or linear auxiliary space depending on the method used.
What is the best approach for Mirror Distance of an Integer?
The best approach is mathematical digit reversal. Extract digits using modulo (n % 10), build the mirrored number by multiplying the result by 10 and adding the digit, then compute the absolute difference. This runs in O(d) time and O(1) space, where d is the number of digits.
Is Mirror Distance of an Integer asked at Google/Amazon/Meta?
Problems involving digit manipulation and number reversal appear frequently in interviews at large tech companies. While this exact problem may vary, similar tasks that require reversing digits, computing differences, or simulating number transformations are common screening questions.
What data structure is used in Mirror Distance of an Integer?
No complex data structure is required. The optimal solution relies on basic arithmetic operations and integer variables. Some implementations temporarily use strings for convenience when reversing digits.
What is the time complexity of Mirror Distance of an Integer?
The time complexity is O(d), where d is the number of digits in the integer. Each digit is processed exactly once when constructing the mirrored number. Both the string-based and arithmetic approaches have the same time complexity.

Ready to solve this problem?

Practice Mirror Distance of an Integer with our built-in code editor and test cases.

Practice on FleetCode