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
- In
digitSum, create anintnamedtotal, starting at 0. - Write a
whileloop that runs as long asnis greater than 0. - Inside the loop, add
n % 10tototal. - Then store
n / 10back inn. Without this line the loop never ends. - After the loop, return
total. - In
biggestDigitSum, create anintnamedbest, starting atnums[0]. - Loop over
numswith anintnamednum. - If
digitSum(num)is greater thandigitSum(best), setbesttonum. - 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
nis 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,
bestkeeps 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, notdigitSum(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
bestat 0. If every number had a digit sum of 0 that would be harmless, but starting atnums[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
