KatInterview

258. Add Digits

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to repeatedly sum the digits of a non-negative integer num until the result is a single-digit number. This resulting single-digit number is also known as the digital root of the original number. The follow-up question challenges us to find a solution that runs in O(1) time, without using any loops or recursion, which hints at a mathematical approach rather than a straightforward simulation.

Solution

Add Digits in Python

class Solution:
    def addDigits(self, num: int) -> int:
        # This problem is equivalent to finding the digital root of a number.
        # The digital root is a concept from number theory.
        # A number and the sum of its digits have the same remainder when divided by 9.
        # n 
≡ sum_of_digits(n) (mod 9)
        
        # Base case: if num is 0, the result is 0.
        if num == 0:
            return 0
        
        # For any positive integer num, the digital root can be calculated
        # using the formula: 1 + (num - 1) % 9.
        # This formula handles both cases for num > 0:
        # 1. num is a multiple of 9: e.g., num=18 -> 1 + (17 % 9) = 1 + 8 = 9.
        # 2. num is not a multiple of 9: e.g., num=38 -> 1 + (37 % 9) = 1 + 1 = 2.
        return 1 + (num - 1) % 9

Add Digits in Java

class Solution {
    public int addDigits(int num) {
        // The problem asks for the digital root of a number.
        // This has a mathematical property related to modulo 9.

        // Base case: if num is 0, the result is 0.
        if (num == 0) {
            return 0;
        }

        // For any positive integer num, the digital root can be calculated
        // using the formula: 1 + (num - 1) % 9.
        // This formula elegantly combines two cases for num > 0:
        // 1. If num is a multiple of 9 (e.g., 18), its digital root is 9.
        //    Calculation: 1 + (18 - 1) % 9 = 1 + 17 % 9 = 1 + 8 = 9.
        // 2. If num is not a multiple of 9 (e.g., 38), its digital root is num % 9.
        //    Calculation: 1 + (38 - 1) % 9 = 1 + 37 % 9 = 1 + 1 = 2.
        return 1 + (num - 1) % 9;
    }
}

Add Digits in C++

class Solution {
public:
    int addDigits(int num) {
        // The problem is to find the digital root of a number.
        // The digital root has a property related to modulo 9.
        // For any integer n, n and the sum of its digits are congruent modulo 9.
        // i.e., n 
≡ sum_of_digits(n) (mod 9).
        
        // Base case: if num is 0, the result is 0.
        if (num == 0) {
            return 0;
        }
        
        // For any positive integer num, the digital root can be calculated
        // using the formula: 1 + (num - 1) % 9.
        // This formula handles both cases for num > 0:
        // 1. num is a multiple of 9: e.g., num=18 -> 1 + (17 % 9) = 1 + 8 = 9.
        // 2. num is not a multiple of 9: e.g., num=38 -> 1 + (37 % 9) = 1 + 1 = 2.
        return 1 + (num - 1) % 9;
    }
};

Add Digits in TypeScript

function addDigits(num: number): number {
    // The problem is asking for the digital root of a number.
    // This has a mathematical property related to modulo 9.
    
    // Handle the edge case where num is 0.
    if (num === 0) {
        return 0;
    }

    // For any positive integer, the digital root can be found using the formula:
    // 1 + (num - 1) % 9
    // This elegantly handles multiples of 9 (which result in 9) and
    // other numbers (which result in num % 9).
    // Example 1 (multiple of 9): num = 18 -> 1 + (17 % 9) -> 1 + 8 = 9
    // Example 2 (not multiple of 9): num = 38 -> 1 + (37 % 9) -> 1 + 1 = 2
    return 1 + (num - 1) % 9;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.