Given an integer n (in base 10) and a base k, return the sum of the digits of n after converting n from base 10 to base k.
After converting, each digit should be interpreted as a base 10 number, and the sum should be returned in base 10.
Example 1:
Input: n = 34, k = 6 Output: 9 Explanation: 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.
Example 2:
Input: n = 10, k = 10 Output: 1 Explanation: n is already in base 10. 1 + 0 = 1.
Constraints:
1 <= n <= 1002 <= k <= 10Problem Overview: Given an integer n and a base k, convert n from base 10 into base k and return the sum of its digits in that base. The challenge is performing the base conversion efficiently without building unnecessary data structures.
Approach 1: Modulo and Division Method (O(logk n) time, O(1) space)
This approach directly simulates base conversion using arithmetic. Repeatedly take n % k to extract the least significant digit in base k, add it to a running sum, then update n = n / k. Each iteration removes one base‑k digit from the number. The process stops when n becomes zero. Since every step reduces the number by a factor of k, the loop runs about logk(n) times.
This is the most practical solution because it avoids storing the converted digits. Instead of building the entire representation first, you accumulate the sum as digits are generated. The algorithm uses only constant extra memory and performs simple arithmetic operations, making it extremely efficient for large inputs. This technique relies on basic math principles behind positional number systems.
Approach 2: Recursive Conversion (O(logk n) time, O(logk n) space)
The recursive approach mirrors how base conversion is often described mathematically. The idea is simple: the digit sum of n in base k equals (n % k) + sum(n / k). Each recursive call processes one digit and reduces the number by dividing it by k. The recursion stops when n becomes zero.
This version is conceptually elegant and clearly expresses the relationship between the number and its base‑k digits. However, it uses the call stack to maintain intermediate states, resulting in O(logk n) auxiliary space. For languages with limited recursion depth or performance-sensitive environments, the iterative method is typically preferred. Still, recursion is useful for demonstrating the structure of base conversion and practicing recursion patterns built on simple mathematical decomposition.
Recommended for interviews: The modulo and division method is the expected solution. It shows that you understand how base conversion works internally and can implement it with constant space. Mentioning the recursive form demonstrates deeper conceptual understanding, but the iterative version is cleaner and avoids unnecessary stack usage.
This approach involves converting the number from base 10 to base k by repeatedly dividing the number by k and taking the remainder. The remainders give the digits of the number in base k, and these digits are then summed up.
The function sumOfDigitsInBaseK takes an integer n and a base k. It then converts n to base k by repeatedly dividing n by k, taking the remainder as the digit. These digits are added together to get the sum of digits in base k.
Time Complexity: O(logk(n)), because at each step we divide the number by k.
Space Complexity: O(1), as we use only a fixed amount of extra space.
This approach uses a recursive function to convert the number into base k and sum the digits. Each recursive call reduces the problem size by one digit.
The recursive function sumOfDigitsInBaseKRecursive computes the sum of digits of n in base k by recursively dividing n and accruing remainders.
Time Complexity: O(logk(n)), equivalent to the iterative method due to the number of recursive calls.
Space Complexity: O(logk(n)), due to the call stack.
We divide n by k and take the remainder until it is 0. The sum of the remainders gives the result.
The time complexity is O(log_{k}n), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
Rust
JavaScript
C
| Approach | Complexity |
|---|---|
| Modulo and Division Method | Time Complexity: O(logk(n)), because at each step we divide the number by k. |
| Recursive Conversion | Time Complexity: O(logk(n)), equivalent to the iterative method due to the number of recursive calls. |
| Mathematics | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Modulo and Division Method | O(log_k n) | O(1) | Best general solution. Efficient base conversion without storing digits. |
| Recursive Conversion | O(log_k n) | O(log_k n) | Useful for explaining the mathematical structure of base conversion or practicing recursion. |
Sum of Digits in Base K | Weekly Contest 238 | Leetcode | Easy Problems 1837 • TECH_ED • 748 views views
Watch 9 more video solutions →Practice Sum of Digits in Base K with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor