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
- In
digitSum, createvar num = nandvar total = 0. - Loop
while (num > 0). - Inside, add
num % 10tototal, then setnumtonum / 10. Without the second line,numnever changes and the loop never ends. - Return
total. - In
biggestDigitSum, createvar best = nums[0]. - Loop
for (num in nums). IfdigitSum(num) > digitSum(best), setbest = num. - 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 return28instead of19. - 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 avarcopy. - Starting
bestat 0 instead ofnums[0]. The answer must be one of the array's numbers. Starting fromnums[0]guarantees that, and also makes the earliest number win a tie.
