LeetCode Problem

How to Solve Add Digits

In the Add Digits problem, the goal is to repeatedly sum the digits of an integer until a single digit is achieved. While a direct simulation approach works, optimization using number theory can help avoid unnecessary iterations and improve efficiency.

GhostInterview Help

Need help with Add Digits without spending extra time grinding it?

GhostInterview can read Add Digits from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #258Math plus SimulationReviewed 2026-03-08
Difficulty
Easy
Primary pattern
Math plus Simulation
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

In the Add Digits problem, the goal is to repeatedly sum the digits of an integer until a single digit is achieved. While a direct simulation approach works, optimization using number theory can help avoid unnecessary iterations and improve efficiency.

Problem Statement

Given an integer num, you need to repeatedly add all of its digits until the result is a single digit, and then return that digit. This process continues until only one digit remains in the number.

For example, if num = 38, the process goes as follows: 3 + 8 = 11, and then 1 + 1 = 2. Since 2 is a single digit, it will be returned as the result. Another example: if num = 0, the result is 0.

Examples

Example 1

Input: num = 38

Output: 2

The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it.

Example 2

Input: num = 0

Output: 0

Example details omitted.

Constraints

  • 0 <= num <= 231 - 1

Solution Approach

Simulation Approach

A straightforward solution involves simulating the repeated addition of digits. In each step, the sum of the digits of the number is calculated until the number becomes a single digit. This approach directly implements the problem statement.

Mathematical Approach

Using a mathematical trick, the problem can be solved without iterating through the digits. The digital root formula simplifies the process, using the modulo operation. This method leverages properties of numbers and avoids unnecessary repetition.

Optimized Approach

For numbers greater than zero, the digital root can be calculated directly using num % 9. If num is 0, the result is 0; otherwise, the result is num % 9, except when num % 9 == 0, in which case the result is 9.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The simulation approach has a time complexity of O(log n) because the number of digits reduces at each iteration. The mathematical approach and optimized approach both have a time complexity of O(1) as they use constant-time operations. All approaches use O(1) space complexity as they do not require additional memory proportional to the input size.

What Interviewers Usually Probe

  • Candidate should show understanding of basic digit operations and how to optimize them.
  • Look for recognition of number properties like digital roots and their use in optimization.
  • Expect exploration of both brute force and efficient approaches, demonstrating a clear trade-off analysis.

Common Pitfalls or Variants

Common pitfalls

  • Misunderstanding the problem and applying unnecessary loops or calculations.
  • Not recognizing the mathematical shortcut using modulo operations to directly compute the result.
  • Forgetting edge cases like num = 0 or not handling the case where num % 9 == 0 correctly.

Follow-up variants

  • Apply the same concept to larger numbers where the number of iterations grows.
  • Solve the problem using recursion instead of iteration.
  • Handle a list of numbers and apply the same digit-sum reduction approach.

How GhostInterview Helps

  • GhostInterview helps by walking you through the most efficient approach, teaching you the mathematical shortcuts like the digital root.
  • By explaining the key trade-offs, GhostInterview ensures you're aware of when to optimize and when a simple approach is sufficient.
  • It provides clear, actionable insights that allow you to master both simulation and mathematical solutions effectively.

Topic Pages

FAQ

How can I optimize the Add Digits problem?

You can optimize the Add Digits problem using a digital root formula with modulo 9, reducing the time complexity to O(1).

What is the main pattern in the Add Digits problem?

The main pattern is using mathematical operations like summing digits repeatedly, which can be optimized using number theory principles.

What happens if num is 0?

If num is 0, the result is 0, as there are no digits to sum.

How does the digital root formula work?

The digital root formula simplifies the problem by computing num % 9. If the result is 0 and num > 0, the result is 9; otherwise, it's num % 9.

Is there a more efficient approach than simulation?

Yes, using the digital root method with modulo operations is more efficient than simulation, achieving O(1) time complexity.

GhostInterview Solver

Need direct help with Add Digits instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Add Digits from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.