LeetCode in Kotlin

3300. Minimum Element After Replacement With Digit Sum

Easy

You are given an integer array nums.

You replace each element in nums with the sum of its digits.

Return the minimum element in nums after all replacements.

Example 1:

Input: nums = [10,12,13,14]

Output: 1

Explanation:

nums becomes [1, 3, 4, 5] after all replacements, with minimum element 1.

Example 2:

Input: nums = [1,2,3,4]

Output: 1

Explanation:

nums becomes [1, 2, 3, 4] after all replacements, with minimum element 1.

Example 3:

Input: nums = [999,19,199]

Output: 10

Explanation:

nums becomes [27, 10, 19] after all replacements, with minimum element 10.

Constraints:

Solution

import kotlin.math.min

class Solution {
    fun minElement(nums: IntArray): Int {
        var min = Int.Companion.MAX_VALUE
        for (x in nums) {
            min = min(min, solve(x))
        }
        return min
    }

    private fun solve(x: Int): Int {
        var x = x
        var sum = 0
        while (x != 0) {
            sum += x % 10
            x /= 10
        }
        return sum
    }
}