Add Digits (LeetCode 258): Solution and Explanation
Keep replacing a non-negative integer with the sum of its digits until one digit is left, and return that digit. The loop is easy; the follow-up asks for O(1) time, which needs the digital root formula.
Intuition
The direct approach is a loop: split the number into digits, add them, and repeat while the result has two or more digits. For any 32-bit input this finishes in at most three rounds, so it is already fast, but the follow-up asks for a closed form.
Look at what a digit sum does to the remainder mod 9. Write 38 as 3 * 10 + 8. Since 10 = 9 + 1, that is 3 * 9 + (3 + 8), so 38 and 3 + 8 differ by a multiple of 9. The same holds for every power of 10, so every number has the same remainder mod 9 as its digit sum.
That remainder survives every round, so the final single digit is the one digit from 1 to 9 with the same remainder as num. The only wrinkle is that a remainder of 0 means the digit 9 (for 18, 99, 999 and so on), and the input 0 itself, which stays 0. The expression 1 + (num - 1) % 9 maps remainder 0 to 9 and leaves the others alone, so only num = 0 needs its own check.
Approach: Digital root formula
- If
numis 0, return 0. It is the only input whose digit sum is 0. - Otherwise compute
(num - 1) % 9, which is 0 to 8. - Return that value plus 1. Multiples of 9 come out as 9 and every other number comes out as
num % 9.
Step-by-step example
num = 38
- The slow way: 3 + 8 = 11, which still has two digits, then 1 + 1 = 2.
- 38, 11 and 2 all leave remainder 2 when divided by 9. The remainder never changed.
- In the code, num is not 0, so skip the early return.
- (38 - 1) % 9 = 37 % 9 = 1, and 1 + 1 = 2. Return 2 without any loop.
Code: Python, Java, C++
1class Solution:
2 def addDigits(self, num: int) -> int:
3 if num == 0:
4 return 0
5 return 1 + (num - 1) % 91class Solution {
2 public int addDigits(int num) {
3 if (num == 0) return 0;
4 return 1 + (num - 1) % 9;
5 }
6}1class Solution {
2public:
3 int addDigits(int num) {
4 if (num == 0) return 0;
5 return 1 + (num - 1) % 9;
6 }
7};Time and space complexity
O(1)
One comparison, one subtraction, one modulo and one addition, whatever the size of num.
O(1)
No extra storage beyond a few integers.
Other approaches
Simulate the digit sums (time O(log num), space O(1))
Loop while num >= 10, replacing it with the sum of its digits. It follows the statement literally and is easy to trust. Each round shrinks the number to about 9 times its digit count, so it needs only a handful of rounds, but it does not meet the no-loop follow-up.
Plain mod 9 with a fix-up (time O(1), space O(1))
Compute num % 9 and return 9 when that is 0 and num is positive. It is the same idea as the formula written with an explicit branch.
Common mistakes
- Returning
num % 9directly. That gives 0 for 9, 18 and 99, whose answer is 9. - Using
1 + (num - 1) % 9without the zero check. In Python it returns 9 for num = 0 (because -1 % 9 is 8), and in Java or C++ it returns 0 only by accident of negative remainders. - Converting the number to a string on every round of the loop version. It works, but integer division by 10 is simpler and avoids allocation.
Frequently asked questions
Why does a number have the same remainder mod 9 as its digit sum?
Each power of 10 is one more than a multiple of 9 (10 = 9 + 1, 100 = 99 + 1, and so on). So a digit d in any position contributes d plus a multiple of 9, and the whole number equals its digit sum plus a multiple of 9.
What is a digital root?
It is the name for exactly what this problem returns: the single digit reached by summing digits repeatedly. For positive numbers it equals 1 + (n - 1) mod 9.
How many rounds does the loop version need?
The largest digit sum of a 10-digit number is 90, the largest for a two-digit number is 18, so any 32-bit input finishes in at most three rounds, for example 1999999999 -> 82 -> 10 -> 1.