3783. Mirror Distance of an Integer
Difficulty: Easy
Topics: Mid Level, Math, Weekly Contest 481
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) = 01which 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.
-
Example 4:
- Input: n = 100
- Output: 99
Constraints:
1 <= n <= 10⁹
Hint:
- Simulate as described
Solution:
The function calculates the mirror distance of an integer by reversing its digits, then returning the absolute difference between the original number and its reverse.
Approach:
- Convert the integer to its digit-reversed form without converting to a string.
- Use a loop to extract digits from the original number and build the reversed number.
- Return the absolute difference between the original and reversed numbers.
Let's implement this solution in PHP: 3783. Mirror Distance of an Integer
<?php
/**
* @param Integer $n
* @return Integer
*/
function mirrorDistance(int $n): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo mirrorDistance(25) . "\n"; // Output: 27
echo mirrorDistance(10) . "\n"; // Output: 9
echo mirrorDistance(7) . "\n"; // Output: 0
echo mirrorDistance(100) . "\n"; // Output: 99
?>
Explanation:
-
Reverse the digits:
- Repeatedly take the last digit of
nusingn % 10. - Append it to
reversedby multiplyingreversedby 10 and adding the digit. - Remove the last digit from
nusing integer division by 10. - Continue until
nbecomes 0.
- Repeatedly take the last digit of
-
Handle leading zeros:
- The reversal process automatically discards leading zeros because they don’t contribute to the integer value (e.g.,
10→1).
- The reversal process automatically discards leading zeros because they don’t contribute to the integer value (e.g.,
-
Compute mirror distance:
- Use
abs(original - reversed)to get the positive difference.
- Use
Complexity
-
Time Complexity: O(d) where d is the number of digits in
n(at most 10, since n ≤ 10⁹). - Space Complexity: O(1) — only a few integer variables used.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)