1837. Sum of Digits in Base K LeetCode Solution
In this guide, you will get 1837. Sum of Digits in Base K LeetCode Solution with the best time and space complexity. The solution to Sum of Digits in Base K problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Sum of Digits in Base K solution in C++
- Sum of Digits in Base K solution in Java
- Sum of Digits in Base K solution in Python
- Additional Resources

Problem Statement of Sum of Digits in Base K
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 <= 100
2 <= k <= 10
Complexity Analysis
- Time Complexity: O(\log n)
- Space Complexity: O(1)
1837. Sum of Digits in Base K LeetCode Solution in C++
class Solution {
public:
int sumBase(int n, int k) {
int ans = 0;
while (n > 0) {
ans += n % k;
n /= k;
}
return ans;
}
};
/* code provided by PROGIEZ */
1837. Sum of Digits in Base K LeetCode Solution in Java
class Solution {
public int sumBase(int n, int k) {
int ans = 0;
while (n > 0) {
ans += n % k;
n /= k;
}
return ans;
}
}
// code provided by PROGIEZ
1837. Sum of Digits in Base K LeetCode Solution in Python
class Solution:
def sumBase(self, n: int, k: int) -> int:
ans = 0
while n > 0:
ans += n % k
n //= k
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.