Algorithm - Encoding Topic
Introduction
I recently came across a brainteaser while doing interview prep:
Q: Using a balance scale, how can you weigh every integer mass from 1–121g with the fewest weights, and what should those weights be?
A: 1, 3, 9, 27, 81g—balanced ternary.
It reminded me of a LeetCode problem I once practiced with a friend—168. Excel Sheet Column Title. On my first try I only solved it after a hint.
After comparing the two today, I realized my struggle came from not linking quotient/remainder to each digit position. Reflecting on both together finally made it click.
Background
Number Bases and Encoding Systems (from Basic to Advanced)
Level | Type | Digit/Symbol Range | Example | Carry Rule | Typical Use | Related LeetCode | Key Feature | ||
---|---|---|---|---|---|---|---|---|---|
🟢 1 | Binary (Base-2) | {0, 1} | 13₁₀ = 1101₂ | Carry at 2 | Computers, logic circuits | 476. Number Complement, 1018. Binary Prefix Divisible by 5 | Simplest positional base | ||
🟢 2 | Octal (Base-8) | {0–7} | 83₁₀ = 123₈ | Carry at 8 | File permissions, early systems | (Conceptual, no direct LeetCode) | Compact early computing format | ||
🟢 3 | Decimal (Base-10) | {0–9} | Regular numbers | Carry at 10 | Human arithmetic | 66. Plus One, 43. Multiply Strings | Natural counting system | ||
🟡 4 | Hexadecimal (Base-16) | {0–9, A–F} | 255₁₀ = FF₁₆ | Carry at 16 | Memory addresses, color codes | 405. Convert a Number to Hexadecimal | Dense, compact data representation | ||
🟡 5 | Alphabetic Base-26 (A–Z) | {A–Z → 1–26} | 28₁₀ → AB | Carry at 26 (no zero) | Excel columns, labeling | 168. Excel Sheet Column Title, 171. Excel Sheet Column Number | Non-zero base encoding | ||
🟠 6 | Ternary (Base-3) | {0, 1, 2} | 40₁₀ = 1111₃ | Carry at 3 | Theoretical computing, puzzles | (Indirectly in base-conversion topics) | Foundation of balanced ternary | ||
🟠 7 | Balanced Ternary (Base 3) | {−1, 0, +1} | 5₁₀ → + − − | Carry at 3 (2 → −1 +1 carry) | Weighing puzzle (1–121g), symmetric math | (Conceptually related to 1017. Convert to Base −2) | Symmetric representation, no sign bit | ||
🔵 8 | Negative Base (−2, −3) | {0, 1} | 2₁₀ = 11010₍₋₂₎ | Carry at | b | but sign alternates | Theoretical computing | 1017. Convert to Base −2 | Uses alternating sign powers |
🔵 9 | Balanced Quaternary (Base 4) | {−2 … +2} | 6₁₀ → −2, +2 | Carry at 4 (>2 → balanced) | Signal encoding, circuits | (Conceptual, no direct LeetCode) | Wider symmetric digit range |
Mathematical Expression of Positional Notation
For any base (b):
$N = \sum_{i=0}^{n} d_i \times b^i$
where each digit ($d_i$) satisfies ($0 \leq d_i < b$).
Example
$(1101)2 = 1×2^3 + 1×2^2 + 0×2^1 + 1×2^0 = 13{10}$
Balanced form
$ N = \sum_{i=0}^{n} d_i \times b^i, \quad d_i \in [−k, + k] $
(e.g., for balanced ternary → (b=3, k=1))
Manual Conversion Methods
Remainder → digit, Quotient → carry upward, Carry → adjustment to keep digits valid.
We subtract 1 before division to map digits from 0–25 to 1–26. In balanced ternary, we only adjust the upper half of the digit range to keep the space symmetric, so we manipulate the carry instead.
🔹 1. Decimal → Other Base (Repeated Division & Remainder)
Steps
- Divide the number by the target base (b).
- Record the remainder (r).
- Continue dividing the quotient by (b) until 0.
- Read remainders from bottom to top.
Example
13 → Binary
13 ÷ 2 = 6 R1
6 ÷ 2 = 3 R0
3 ÷ 2 = 1 R1
1 ÷ 2 = 0 R1
→ 1101₂
🔹 3. Decimal → Balanced Base (Remainder Adjustment)
Algorithm
- Compute ($r = n \bmod b$).
- If (r > b/2), set current digit = (r − b) and carry +1.
- Otherwise, keep r and divide (n //= b).
- Repeat until (n=0).
- Combine digits from bottom → top.
Example
5 → Balanced Ternary
5 ÷ 3 = 1 R2 → change 2 to −1, carry +1
1+1=2 → change 2 to −1, carry +1
Result: + − −
Check: 9 − 3 − 1 = 5
🔹 4. Excel-Style Base-26 (A–Z Encoding)
Because there’s no zero, we adjust before division:
n -= 1
r = n % 26
ch = chr(r + ord('A'))
n //= 26
Example: 28 → “AB”