Decode a Mixed-Radix Positional Number
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Tests the ability to convert a number represented in a variable base-per-digit (mixed-radix) system back to its standard base-10 integer value.
Decode a Mixed-Radix Positional Number
A mixed-radix number system generalizes ordinary positional notation by allowing each digit position to have its own base (radix) instead of a single shared base.
Formally, given a sequence of bases b[0], b[1], ..., b[n-1] and a corresponding sequence of digits d[0], d[1], ..., d[n-1], the decimal value is computed as:
value = d[0] * (b[1] * b[2] * ... * b[n-1])
+ d[1] * (b[2] * b[3] * ... * b[n-1])
+ ...
+ d[n-2] * b[n-1]
+ d[n-1]
In other words, position i (0-indexed from the left / most-significant side) has a place value equal to the product of all bases that come after it: b[i+1] * b[i+2] * ... * b[n-1]. The last position always has place value 1.
This is identical to ordinary base conversion when all bases are equal (e.g., all 10 gives decimal, all 2 gives binary), but here each b[i] may differ.
Your task: Given bases and digits, return the non-negative integer they represent in base 10.
Validity guarantee: For every position i, 0 <= digits[i] < bases[i]. You do not need to validate inputs.
Constraints
1 <= len(bases) == len(digits) <= 20002 <= bases[i] <= 10^4for alli0 <= digits[i] < bases[i]for alli- The answer may be very large; return the exact Python integer (no modular reduction).
- If
len(bases) == 1, there is exactly one digit; its place value is 1, so the answer equalsdigits[0].
Examples
Example 1
Input: decode_mixed_radix([10, 10, 10], [3, 7, 5])
Output: 375
Example 2
Input: decode_mixed_radix([2, 2, 2, 2], [1, 0, 1, 1])
Output: 11
Example 3
Input: decode_mixed_radix([24, 60, 60], [1, 30, 45])
Output: 5445
Hints
Hint 1
Process digits from right to left (least-significant to most-significant), keeping a running place value that you multiply by the next base as you move left.
Hint 2
Alternatively, use Horner's-method-style evaluation left to right: accumulate `result = result * bases[i] + digits[i]`, which avoids storing the full place value.
Hint 3
Edge cases to watch: a single-element list (answer is just digits[0]), and all-zero digits (answer is 0 regardless of bases).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight: Horner's Method for Mixed-Radix
In an ordinary base-b number d[0] d[1] ... d[n-1], the value is:
d[0]*b^(n-1) + d[1]*b^(n-2) + ... + d[n-1]*b^0
Horner's method rewrites this as nested multiplications, left to right:
result = (...((d[0] * b + d[1]) * b + d[2]) * b + ...) + d[n-1]
The mixed-radix generalization is identical, except each multiplication uses the base of the current position rather than a fixed b:
result = (...((d[0] * b[1] + d[1]) * b[2] + d[2]) * b[3] + ...) + d[n-1]
Or, formulated slightly differently for the loop:
result = 0
for i in range(n):
result = result * bases[i] + digits[i]
Why this works: After processing position i, result holds the numeric value of the sub-number d[0..i] in the mixed-radix subsystem with bases b[0..i]. Multiplying by bases[i+1] (handled in the next iteration as bases[i] at that step) "shifts" the accumulated value left by one mixed-radix position, exactly scaling it by the next base's contribution.
This matches the direct place-value formula:
value = Σ d[i] * (b[i+1] * b[i+2] * ... * b[n-1]) for i = 0..n-1
where the last term has an empty product = 1.
Correctness on Edge Cases
- Single digit: Loop runs once;
result = 0 * bases[0] + digits[0] = digits[0]. Correct. - All-zero digits: Every iteration adds 0; result stays 0. Correct.
- Large values: Python integers are arbitrary-precision, so no overflow.
Complexity
| Time | O(n) — one pass through the arrays |
| Space | O(1) extra — only a scalar accumulator |
Reference Solution
from typing import List
def decode_mixed_radix(bases: List[int], digits: List[int]) -> int:
result = 0
for i in range(len(bases)):
result = result * bases[i] + digits[i]
return result
Sample Walkthroughs
Sample 1 — bases=[10,10,10], digits=[3,7,5]
Standard decimal: 3×100 + 7×10 + 5 = 375
Horner: ((0×10+3)×10+7)×10+5 = (3×10+7)×10+5 = 37×10+5 = 375 ✓
Sample 2 — bases=[2,2,2,2], digits=[1,0,1,1]
Binary 1011 = 8+0+2+1 = 11
Horner: (((1)×2+0)×2+1)×2+1 = (2×2+1)×2+1 = 5×2+1 = 11 ✓
Sample 3 — bases=[24,60,60], digits=[1,30,45]
Hours-minutes-seconds: 1 hour, 30 min, 45 sec
= 1×3600 + 30×60 + 45 = 3600+1800+45 = 5445 seconds ✓