Difficulty: Easy | Acceptance: 91.50% | Paid: No Topics: Math
Problem Description
Given an integer n, find its mirror (the number formed by reversing its digits) and return the absolute difference between n and its mirror.
For example, the mirror of 123 is 321, and the mirror distance is |123 - 321| = 198.
Table of Contents
- Examples
- Constraints
- String Manipulation
- Mathematical Reversal
- Recursive Reversal
Examples
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
-10⁹ <= n <= 10⁹
String Manipulation
Intuition Convert the number to a string, reverse it, and convert back to an integer to find the mirror, then calculate the absolute difference.
Steps
- Extract the sign of the number
- Convert the absolute value to a string
- Reverse the string and convert back to integer
- Apply the original sign to get the mirror
- Return the absolute difference between n and its mirror
class Solution:
def mirrorDistance(self, n: int) -> int:
sign = -1 if n < 0 else 1
mirror = int(str(abs(n))[::-1]) * sign
return abs(n - mirror)
Complexity
- Time: O(log n) - Converting to string and reversing takes time proportional to the number of digits
- Space: O(log n) - String representation of the number
- Notes: Simple and readable, but uses extra space for string conversion
Mathematical Reversal
Intuition Extract digits from the number using modulo and division operations, then build the reversed number mathematically.
Steps
- Extract the sign of the number
- Use modulo 10 to get the last digit
- Build the mirror by multiplying by 10 and adding each digit
- Apply the original sign to get the mirror
- Return the absolute difference between n and its mirror
class Solution:
def mirrorDistance(self, n: int) -> int:
sign = -1 if n < 0 else 1
abs_n = abs(n)
mirror = 0
while abs_n > 0:
mirror = mirror * 10 + abs_n % 10
abs_n //= 10
mirror *= sign
return abs(n - mirror)
Complexity
- Time: O(log n) - We process each digit exactly once
- Space: O(1) - Only using a constant amount of extra space
- Notes: Most efficient approach with constant space complexity
Recursive Reversal
Intuition Use recursion to reverse the digits by processing the last digit first and building the reversed number.
Steps
- Extract the sign of the number
- Define a recursive function that takes the remaining number and the current reversed value
- Base case: when the number is 0, return the reversed value
- Recursive case: extract the last digit and add it to the reversed value, then recurse with the remaining digits
- Apply the original sign to get the mirror
- Return the absolute difference between n and its mirror
class Solution:
def mirrorDistance(self, n: int) -> int:
sign = -1 if n < 0 else 1
abs_n = abs(n)
def reverse(num, rev):
if num == 0:
return rev
return reverse(num // 10, rev * 10 + num % 10)
mirror = reverse(abs_n, 0) * sign
return abs(n - mirror)
Complexity
- Time: O(log n) - We process each digit exactly once
- Space: O(log n) - Recursion stack depth equals the number of digits
- Notes: Elegant solution but uses stack space due to recursion