Start learning free

LESSON 248 OF 677 · HELPER FUNCTIONS

Biggest Digit Sum in Kotlin: Using a Helper

Practice Read the full explanation

You get an array of integers nums. Return the number whose digits add up to the most.

The digit sum of 472 is 4 + 7 + 2 = 13. If two numbers have the same digit sum, return the one that comes first.

The work is split between two functions. The first one is a helper. It gets one number and returns the sum of its digits. The second one is the function that is graded. It uses the helper to compare the numbers.

 

Example 1:

Input: nums = [472, 95, 1000]
Output: 95
Explanation: The digit sums are 13, 14 and 1.
95 has the biggest one: 9 + 5 = 14.

Example 2:

Input: nums = [19, 55, 28]
Output: 19
Explanation: All three digit sums are 10.
19 comes first, so it wins the tie.

Example 3:

Input: nums = [0, 7]
Output: 7
Explanation: The digit sum of 0 is 0.
The digit sum of 7 is 7.

 

Constraints:

  • nums has at least one number.
  • Every number is between 0 and 109.
  • Return the number itself, not its digit sum.
STEP 1 OF 10
    Ctrl + Enter to check

    KOTLIN · HELPER FUNCTIONS

    Biggest Digit Sum in Kotlin, explained

    Problem overview

    The function biggestDigitSum gets an array of integers nums and returns the number whose digits add up to the most. The digit sum of 472 is 4 + 7 + 2 = 13. If two numbers have the same digit sum, the one that comes first wins. The function returns the number itself, not its digit sum. The array has at least one number, and every number is between 0 and 1,000,000,000.

    For [472, 95, 1000] the digit sums are 13, 14 and 1, so the answer is 95. For [19, 55, 28] all three sums are 10, so the first number, 19, wins the tie. For [0, 7] the sums are 0 and 7, so the answer is 7.

    The work is split between two functions. digitSum is a helper that gets one number and returns the sum of its digits. biggestDigitSum is the function the tests call, and it uses the helper to compare the numbers.

    Approach for the helper

    The last digit of a whole number is the remainder when you divide by 10: 472 % 10 is 2. Dividing by 10 with integer division drops the last digit: 472 / 10 is 47, because Int division in Kotlin drops the remainder. So you add the last digit, drop it, and repeat until nothing is left. That is a while loop that runs as long as the number is greater than 0.

    Kotlin function parameters are read-only, so the loop cannot change n. Instead, copy it into a var named num and let the loop take the digits off the copy. For 0 the loop never runs, and the sum stays 0, which is the right answer.

    Approach for the main function

    This is the "largest so far" pattern. Start with the first number as the best, then look at every number and replace the best when a number has a strictly bigger digit sum. Using > and not >= is what makes a tie go to the earlier number.

    Step by step

    1. In digitSum, create var num = n and var total = 0.
    2. Loop while (num > 0).
    3. Inside, add num % 10 to total, then set num to num / 10. Without the second line, num never changes and the loop never ends.
    4. Return total.
    5. In biggestDigitSum, create var best = nums[0].
    6. Loop for (num in nums). If digitSum(num) > digitSum(best), set best = num.
    7. Return best.

    The solution in Kotlin

    class Solution {
        fun digitSum(n: Int): Int {
            var num = n
            var total = 0
            while (num > 0) {
                total += num % 10
                num = num / 10
            }
            return total
        }
    
        fun biggestDigitSum(nums: IntArray): Int {
            var best = nums[0]
            for (num in nums) {
                if (digitSum(num) > digitSum(best)) {
                    best = num
                }
            }
            return best
        }
    }
    

    The helper gives digitSum(472) = 13, digitSum(95) = 14, digitSum(1000) = 1 and digitSum(0) = 0. The three examples print:

    95
    19
    7
    

    The loop starts with the first number again, comparing it with itself. That changes nothing, so there is no need to skip it. The numbers go up to 1,000,000,000, which still fits in a 32-bit Int, so the division and the sum cannot overflow.

    Following the helper

    For 472 the loop takes three turns. In the first, num is 472 and the last digit is 2. In the second, num is 47 and the digit is 7. In the third, num is 4 and the digit is 4. Then num becomes 0, the loop ends, and the total is 2 + 7 + 4 = 13. The biggest allowed number, 1,000,000,000, takes ten turns and has the digit sum 1, because only its leading 1 is not a zero. The number 999,999,999 has the largest digit sum possible here, 81.

    The tie rule in practice

    Take [19, 55, 28]. The loop starts with best = 19, whose digit sum is 10. When it reaches 55, the digit sum is 10, which is not greater than 10, so best stays 19. The same happens for 28. With >= in place of >, 55 would replace 19 and then 28 would replace 55, and the function would return 28. The strict comparison keeps the first number among equals.

    Complexity

    A number with d digits takes d turns in the helper. The main function calls the helper twice per number, so it does roughly twice the digit work of one pass. With at most ten digits per number, the cost is small and grows in step with the length of the array. It uses a constant amount of extra space.

    Common mistakes

    • Using >=. Then a later number with an equal digit sum replaces the earlier one. For [19, 55, 28] the function would return 28 instead of 19.
    • Returning the digit sum. The task asks for the number itself, so return best.
    • Forgetting num = num / 10. The loop would run forever for any number above 0.
    • Trying to change the parameter n. Kotlin does not allow it, which is why the helper makes a var copy.
    • Starting best at 0 instead of nums[0]. The answer must be one of the array's numbers. Starting from nums[0] guarantees that, and also makes the earliest number win a tie.
    Keep going with Kotlin500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free