Watch 10 video solutions for Sum of Digits in Base K, a easy level problem involving Math. This walkthrough by TECH_ED has 748 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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. |