Balanced Digit Sum Across All Bases
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Tests understanding of base conversion by summing digits of a number across every base from 2 to b and checking whether each base yields a digit sum equal to a target value.
Balanced Digit Sum Across All Bases
Given a positive integer n and a target digit sum t, a base b (where b >= 2) is called balanced for n if the sum of the digits of n when written in base b equals exactly t.
Write a function that, given n, t, and an upper bound b_max, returns the list of all balanced bases for n in the range [2, b_max], in ascending order.
How digit-sum in base b works
To find the digit sum of n in base b:
- Repeatedly take
n % bto get the least-significant digit. - Replace
nwithn // b. - Sum all collected digits until
nbecomes 0.
For example, n = 13 in base 3 is 111 (since 1·9 + 1·3 + 1 = 13), so its digit sum in base 3 is 3.
Special case: base ≥ n + 1
When b > n, the representation of n in base b is just the single digit n itself (since n < b). In this situation the digit sum equals n. For all such bases the digit sum is identical, so they are either all balanced (if n == t) or none of them are. Your function should only return the single value b_max as the representative of this group if n == t and b_max >= n + 1; do not list every base from n+1 to b_max.
More precisely: if n == t and b_max >= n + 1, include exactly the value max(n + 1, 2) in the results (representing all bases ≥ n+1 by the smallest such base that is ≤ b_max). Do not include any other values ≥ n+1.
Wait — let's simplify the rule to avoid ambiguity:
Return exactly those bases b in [2, min(b_max, n)] for which the digit sum of n in base b equals t, PLUS, if n == t and b_max >= n + 1, also include n + 1 in the result.
(When n == 1, base 2 gives digit representation 1 with digit sum 1, handled by the general loop; and n+1 = 2 is already in range so no special case needed beyond the formula.)
Constraints
1 <= n <= 10^60 <= t <= 2002 <= b_max <= 10^6- The returned list must be in ascending order.
Examples
Example 1
Input: balanced_bases(13, 3, 20)
Output: [2, 3, 6, 11]
Example 2
Input: balanced_bases(12, 3, 15)
Output: [4, 10]
Example 3
Input: balanced_bases(7, 7, 10)
Output: [8]
Hints
Hint 1
For each base b from 2 to min(b_max, n), compute the digit sum using repeated mod and integer division — this is O(log_b(n)) per base.
Hint 2
The total work across all bases 2..n is O(n) overall because sum of log_b(n) for b=2..n is O(n) by the integral approximation.
Hint 3
Handle the b > n edge case separately: every such base gives digit sum exactly n, so it is balanced if and only if t == n; represent this whole group by the single base n+1 if it is within b_max.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Observations
-
Base conversion digit sum: For a given base
b, we extract digits ofnby repeatedly takingn % b(least-significant digit) and dividingn // b. Sum all collected digits. This runs in O(log_b n) steps. -
Bases larger than n: When
b > n,nitself is a single digit in baseb(since0 <= n < b), so the digit sum is exactlyn. All such bases behave identically — they're either all balanced or none are. We handle this group by including exactlyn+1as a representative ift == nandb_max >= n+1. -
Total complexity: We iterate bases from 2 to
min(b_max, n). For each baseb, the digit sum computation costs O(log_b n). The total cost is:∑{b=2}^{n} log_b(n) = ∑{b=2}^{n} ln(n)/ln(b) ≈ n · ln(n) / ln(n) = O(n)
(The dominant contribution comes from small bases; large bases each cost O(1).)
Space complexity is O(n) worst case for the result list, O(log n) auxiliary.
Correctness
- The loop correctly enumerates every base in
[2, n]and checks the digit sum directly. - The special-case rule for
b > nis sound: sincen < bfor any such base,nhas exactly one digit in that base equal ton, so digit sum =nalways. - We include
n+1(not all ofn+1 … b_max) to avoid O(b_max) output size; the problem statement defines this as the correct behavior.
Edge Cases
n = 1: digit sum in every base ≥ 2 is 1 (since 1 in any base is just1). The loop runs forb=2..min(b_max,1)which is empty (since min(b_max,1)=1 < 2). Thenn+1=2 <= b_maxandt==n==1triggers the special case. So result is[2]ift==1, else[].t = 0: never balanced for any positiven(digit sum of a positive number is always ≥ 1).- Large
b_maxwithb_max >> n: only the special case atn+1can contribute beyond basen.
from typing import List
def balanced_bases(n: int, t: int, b_max: int) -> List[int]:
def digit_sum(num: int, base: int) -> int:
s = 0
while num > 0:
s += num % base
num //= base
return s
result = []
# Check all bases in [2, min(b_max, n)]
upper = min(b_max, n)
for b in range(2, upper + 1):
if digit_sum(n, b) == t:
result.append(b)
# For bases b > n: digit sum is always n (single digit representation)
# Include n+1 as representative if t == n and b_max >= n+1
if n + 1 <= b_max and t == n:
result.append(n + 1)
return result