Add Digits

Easy

Topics
MathSimulation

Given a non-negative integer n, repeatedly add all its digits until the result has only one digit, and return it.

Example 1

38112null
Each node is the digit sum of the one before it, repeated until a single digit is left: 2. Because digit summing preserves the value mod 9, the whole chain collapses to the closed form 1 + (n-1) % 9.
Chain of repeated digit sums.
Input:  n = 38
Output: 2
Explanation: 3 + 8 = 11, 1 + 1 = 2.

Example 2

0null
Input:  n = 0
Output: 0

Constraints

  • 0 <= n <= 2^31 - 1
Editorial — intuition, approach, complexity and pitfalls

Intuition

The literal reading gives the obvious solution: sum the digits, and if the result still has more than one digit, do it again. That loop is correct and fast — each pass shrinks a 10-digit number to at most 90, then to at most 17, then to one digit, so it converges in about four rounds. Perfectly acceptable, and worth writing first.

The closed form comes from noticing why digit sums behave this way. In base 10, 10 ≡ 1 (mod 9), so every power of ten is also congruent to 1. That means a number and its digit sum leave the same remainder when divided by 9:

472 = 4*100 + 7*10 + 2*1
    ≡ 4*1   + 7*1  + 2*1  (mod 9)
    = 13 ≡ 4 (mod 9)

and indeed 4+7+2 = 13, 1+3 = 4

So repeated digit-summing never changes the value mod 9 — it just shrinks the number until only one digit is left. That single digit is therefore completely determined by n mod 9. This is the old "casting out nines" trick.

The only awkwardness is the wrap-around. For n = 9 the answer is 9, not 0, and likewise for 18, 27 and so on. Writing 1 + (n - 1) % 9 shifts the arithmetic into the range 1..9 instead of 0..8, which handles every multiple of 9 without a special case. Zero is handled separately, because 0 is the one input whose digital root really is 0.

Approach

  1. If n == 0, return 0.
  2. Otherwise return 1 + (n - 1) % 9.

Dry Run

n = 38   ->  1 + (37 % 9) = 1 + 1 = 2
             check: 3+8 = 11, 1+1 = 2            ✓

n = 9    ->  1 + (8 % 9)  = 1 + 8 = 9
             the wrap-around case; plain n % 9 would give 0   ✓

n = 199  ->  1 + (198 % 9) = 1 + 0 = 1
             check: 1+9+9 = 19, 1+9 = 10, 1+0 = 1   ✓

n = 0    ->  early return 0                       ✓

Complexity

  • Time — O(1): two arithmetic operations, independent of how large n is. The iterative digit-sum version is O(log n) per pass over a constant number of passes, so O(log n) overall — fine in practice, but this is genuinely constant.
  • Space — O(1): no allocation. Note the iterative version is also O(1) if you sum with arithmetic (n % 10, n //= 10) rather than converting to a string, which would cost O(log n) for the characters.

Common Pitfalls

  • Writing n % 9 directly. It is right for most inputs and wrong for every positive multiple of 9, returning 0 where the answer is 9.
  • Dropping the n == 0 guard. 1 + (-1) % 9 gives 1 in Python, but in Java and C++ the % operator keeps the sign of the dividend, so (-1) % 9 is -1 and the result is 0 by accident rather than by design. Relying on that is fragile across languages.
  • Converting to a string to sum digits. It works, but it allocates, and it obscures that this is a modular-arithmetic fact rather than a string problem.
  • Assuming the formula is a trick with no reasoning behind it. The follow-up is usually "why does that work?", and the answer is 10 ≡ 1 (mod 9).

Full Solution — all languages

class Solution:
    def addDigits(self, n):
        if n == 0:
            return 0
        return 1 + (n - 1) % 9
Asked at Adobe
SensAI
Beta
Tap the mic to stop voice mode