Sum of Digits in Base K - Problem

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: If n = 34 and k = 6, then 34 in base 6 is 54 (since 5×6¹ + 4×6⁰ = 30 + 4 = 34). The sum of digits 5 + 4 = 9.

Input & Output

Example 1 — Basic Conversion
$ Input: n = 34, k = 6
Output: 9
💡 Note: 34 in base 6 is 54 (5×6¹ + 4×6⁰ = 30 + 4 = 34). Sum of digits: 5 + 4 = 9
Example 2 — Base 2 Conversion
$ Input: n = 10, k = 2
Output: 2
💡 Note: 10 in base 2 is 1010 (1×2³ + 0×2² + 1×2¹ + 0×2⁰ = 8 + 0 + 2 + 0 = 10). Sum: 1 + 0 + 1 + 0 = 2
Example 3 — Edge Case Zero
$ Input: n = 0, k = 10
Output: 0
💡 Note: 0 in any base is 0, so sum of digits is 0

Constraints

  • 1 ≤ n ≤ 100
  • 2 ≤ k ≤ 10

Visualization

Tap to expand
Sum of Digits in Base KInputn = 34k = 6Base 10 numberConvertProcess34 → 54₆digits: 5, 4Base 6 conversionSumOutput95 + 4 = 9Sum in base 10Base conversion: 34 ÷ 6 = 5 remainder 4, then 5 ÷ 6 = 0 remainder 5Reading remainders backwards gives us 54₆Final sum: 5 + 4 = 9
Understanding the Visualization
1
Input
Number n in base 10 and target base k
2
Convert
Convert n from base 10 to base k using division
3
Sum
Add all digits in the base k representation
Key Takeaway
🎯 Key Insight: Use modulo operation (n % k) to extract each digit, then divide by k to move to the next digit position.
Asked in
Google 15 Facebook 12 Amazon 8
12.0K Views
Medium Frequency
~8 min Avg. Time
456 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen