commit 31e66beff952415ea1327954d90b45afabe6438e
parent 785e23460e5e020b75b806f6a3d44ba5c870293a
Author: Jack Sangdahl <jack@pngu.org>
Date: Fri, 17 Apr 2026 20:48:35 -0600
3783. mirror distance of an integer
Diffstat:
2 files changed, 55 insertions(+), 0 deletions(-)
diff --git a/mirror_dist_int.c b/mirror_dist_int.c
@@ -0,0 +1,35 @@
+// 3783. Mirror Distance of an Integer
+#include "leetcode.h"
+
+/*
+ * 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'.
+ */
+
+int reverse(int n) {
+ int res = 0;
+ while (n > 0) {
+ res = res * 10 + (n % 10);
+ n /= 10;
+ }
+ return res;
+}
+
+int mirrorDistance(int n) {
+ int m = reverse(n);
+ return abs(m - n);
+}
+
+int main() {
+ int r1 = mirrorDistance(25);
+ int r2 = mirrorDistance(10);
+ int r3 = mirrorDistance(7);
+ printf("%d\n", r1);
+ assert(r1 == 27);
+ printf("%d\n", r2);
+ assert(r2 == 9);
+ printf("%d\n", r3);
+ assert(r3 == 0);
+}
diff --git a/mirror_dist_int.py b/mirror_dist_int.py
@@ -0,0 +1,20 @@
+# 3783. Mirror Distance of an Integer
+
+"""
+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'.
+"""
+
+
+class Solution:
+ def mirrorDistance(self, n: int) -> int:
+ return abs(n - int(str(n)[::-1]))
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.mirrorDistance(n=25))
+ print(obj.mirrorDistance(n=10))
+ print(obj.mirrorDistance(n=7))