Problem overview
biggestDigitSum receives a slice 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 tie, the one that comes first wins. The function 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, so 19 wins by coming first. For [0, 7] the sums are 0 and 7, so the answer is 7.
Approach
A helper, digitSum, turns one number into its digit sum. Then biggestDigitSum works like finding a maximum: remember the best number so far and replace it whenever a number has a strictly bigger digit sum. Using a strict "greater than" is what makes a tie keep the earlier number.
Step by step
The helper digitSum(n):
- Start
total := 0. Forn = 0the loop never runs, so 0 is the right answer. - Go has no
while, so writefor n > 0, a loop with only a condition. - Add the last digit,
n % 10, tototal. For 472,472 % 10is 2. - Drop the last digit with
n = n / 10. Integer division drops the remainder, so 472 becomes 47. Without this line the loop never ends. - Return
total.
The function biggestDigitSum(nums):
- Start
best := nums[0]. The first number is the best one seen before any comparison. - Loop with
for _, num := range nums. The first turn compares the first number with itself, which changes nothing. - If
digitSum(num) > digitSum(best), setbest = num. - Return
bestafter the loop.
The solution in Go
func digitSum(n int) int {
total := 0
for n > 0 {
total += n % 10
n = n / 10
}
return total
}
func biggestDigitSum(nums []int) int {
best := nums[0]
for _, num := range nums {
if digitSum(num) > digitSum(best) {
best = num
}
}
return best
}
Go passes arguments by value, so changing n inside digitSum does not touch the caller's number. That is why the helper can safely use its parameter as a working variable.
Tracing the helper
For 472, the loop runs three times. Printing n, the last digit and the total before each addition gives:
n = 472 last digit 2 total so far 0
n = 47 last digit 7 total so far 2
n = 4 last digit 4 total so far 9
After the third turn, total is 13 and n / 10 is 0, so the loop stops. For n = 0 the condition n > 0 is false from the start, the loop never runs, and the helper returns 0, which is the right digit sum for 0.
The helper assumes n is not negative, as the constraints promise (every number is between 0 and 1,000,000,000). A negative number would skip the loop and report 0.
Why compare through the helper
The main function never looks at digits itself. It asks digitSum for a number's sum and compares the sums. That keeps the "find the best" logic separate from the "add the digits" logic, and each part is easy to read alone.
The helper is called for best again on every turn, which repeats work. For the sizes in this lesson that is fine. If it mattered, you could keep the best digit sum in a second variable and update both together. The reference solution keeps one variable because it is the simpler picture.
Why the number is returned
The function returns best, which is always an element of nums. A common slip is to track the biggest digit sum and return that. For [472, 95, 1000] the biggest sum is 14, but the answer is 95, the number that has it.
The digit sums in the statement's examples can be checked directly: digitSum(95) is 14, digitSum(1000) is 1, and digitSum(19), digitSum(55) and digitSum(28) are all 10, which is the tie in the second example. The largest allowed number, 1,000,000,000, has a digit sum of 1, so it does not overflow or slow anything down: the loop runs once per digit, at most ten times.
Testing
[472 95 1000] -> 95
[19 55 28] -> 19
[0 7] -> 7
digitSum(472) -> 13
digitSum(0) -> 0
Edge cases
A slice with one number returns that number, since the loop has nothing better to find. Zero is a valid number with a digit sum of 0, so a slice like [0, 0] returns the first 0. Numbers with trailing zeros, like 1000, add the zeros as 0 and gain nothing from them. Go's int is 64 bits, so the numbers allowed here fit comfortably and no overflow can occur. The helper also leaves the number you gave it untouched, because Go passes the argument by value.
Common mistakes
- Using
>=. On a tie the later number would replace the earlier one. For[19, 55, 28]the function would return 28. - Returning the digit sum. The function returns the number, so
bestholds a number fromnums. - Forgetting
n = n / 10. The loop would never end. - Starting
bestat 0. That works only if the numbers are non-negative; takingnums[0]is safe for any input.
