LeetCode Entry
3783. Mirror Distance of an Integer
Diff with reversal
3783. Mirror Distance of an Integer easy substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1332
Problem TLDR
Diff with reversal #easy
Intuition
Reverse and subtract. Can’t be done in-place, because we need to know the length of number.
Approach
- Kotlin’s shortest is strings reversal
- Rust doesn’t have divmod, but can use asm that will mutate ‘x’ and return reminder
let d: i32; unsafe { asm!("cdq;div {0}", in(reg) 10, inout("eax") x, out("edx") d) }
Complexity
-
Time complexity: \(O(lg(n))\)
-
Space complexity: \(O(1)\)
Code
// 10ms
fun mirrorDistance(n: Int) =
abs(n - "$n".reversed().toInt())
// 0ms
pub fn mirror_distance(n: i32) -> i32 {
let (mut r, mut x) = (0, n);
while x > 0 { r = r*10+x%10; x/=10}; (r-n).abs()
}