Start learning free

LESSON 244 OF 671 · HELPER FUNCTIONS

Biggest Digit Sum in Java: A Digit Sum Helper Method

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 9
    Ctrl + Enter to check

    JAVA · HELPER FUNCTIONS

    Biggest Digit Sum in Java, explained

    Problem overview

    The method biggestDigitSum receives 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 method returns the number itself, not its digit sum.

    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, and 19 comes first, so 19 wins. For [0, 7] the sums are 0 and 7, so the answer is 7. Every number is between 0 and 1,000,000,000.

    Approach: a helper for one number

    The work is split in two methods. The helper digitSum takes one number and returns the sum of its digits. biggestDigitSum keeps the best number found so far and compares each number with it, using the helper to turn numbers into digit sums.

    To take the digits off a number, use two operations. n % 10 is the last digit: 472 % 10 is 2. n / 10 drops the last digit, because dividing two int values in Java throws away the remainder: 472 / 10 is 47. Repeat until n reaches 0.

    Step by step

    1. In digitSum, create an int named total, starting at 0.
    2. Write a while loop that runs as long as n is greater than 0.
    3. Inside the loop, add n % 10 to total.
    4. Then store n / 10 back in n. Without this line the loop never ends.
    5. After the loop, return total.
    6. In biggestDigitSum, create an int named best, starting at nums[0].
    7. Loop over nums with an int named num.
    8. If digitSum(num) is greater than digitSum(best), set best to num.
    9. After the loop, return best.

    The solution in Java

    class Solution {
        public int digitSum(int n) {
            int total = 0;
            while (n > 0) {
                total += n % 10;
                n = n / 10;
            }
            return total;
        }
    
        public int biggestDigitSum(int[] nums) {
            int best = nums[0];
            for (int num : nums) {
                if (digitSum(num) > digitSum(best)) {
                    best = num;
                }
            }
            return best;
        }
    }

    Some details:

    • The parameter n is a copy of the number that was passed in. Changing it inside the helper doesn't change the caller's number or the array.
    • For 0 the loop never runs, so the helper returns 0, which is the right digit sum.
    • The comparison is strictly greater. On a tie, best keeps the number that came first.
    • The first number is compared with itself in the first turn. That changes nothing.

    Tracing 472: total is 0 and n is 472. After the first turn, total is 2 and n is 47. After the second, total is 9 and n is 4. After the third, total is 13 and n is 0, and the loop ends. Calling the helper on 472, 95 and 1000 returns 13, 14 and 1.

    Edge cases and common mistakes

    • Using >=. With >=, a later number with an equal digit sum replaces the earlier one. For [19, 55, 28] the method would return 28 instead of 19.
    • Returning the digit sum. The method returns the number, best, not digitSum(best).
    • Forgetting n = n / 10. The loop would repeat forever on any positive number.
    • Zeros inside a number. 1000 has digits 1, 0, 0, 0, and the loop adds 0 three times. The digit sum is 1.
    • Starting best at 0. If every number had a digit sum of 0 that would be harmless, but starting at nums[0] is the clean way to cover every case.

    Why the numbers fit

    An int in Java holds values up to about 2.1 billion, so every number the lesson allows fits, and the digit sum stays tiny (at most 81 for a nine-digit number, and 1 for 1,000,000,000). No overflow can happen in the helper.

    Walking through the tie example

    For [19, 55, 28], best starts at 19, whose digit sum is 10. The loop reaches 19 (compares 10 with 10, no change), then 55 (10 is not greater than 10, no change), then 28 (same). The strict comparison never replaces 19, so it is returned. In the first example, best starts as 472 with sum 13. The number 95 has sum 14, which is greater, so best becomes 95. The number 1000 has sum 1 and changes nothing.

    The helper is called twice in each comparison, once for num and once for best. For these small inputs that costs nothing. If you wanted to avoid the repeated work, you could keep the best digit sum in a variable too, but the lesson keeps the code as simple as possible.

    Key points

    Keep the two jobs apart. The helper answers "what is the digit sum of this number?" and knows nothing about arrays or ties. The main method answers "which number wins?" and knows nothing about digits. If the result is wrong, you can test the helper alone on a few numbers such as 0, 7 and 472, and then look at the comparison.

    Testing

    Running the solution on the three examples gives:

    95
    19
    7
    Keep going with Java500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free