LeetCode in Kotlin

3783. Mirror Distance of an Integer

Easy

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:

Example 2:

Input: n = 10

Output: 9

Explanation:

Example 3:

Input: n = 7

Output: 0

Explanation:

Constraints:

Solution

import kotlin.math.abs

class Solution {
    private fun rev(n: Int): Int {
        var n = n
        var a = 0
        while (n > 0) {
            a = a * 10 + (n % 10)
            n /= 10
        }
        return a
    }

    fun mirrorDistance(n: Int): Int {
        val m = rev(n)
        return abs(m - n)
    }
}