Math
How to know which math to use
A quick cheat sheet for deducing the right counting logic:
- Use powers (): when every single item makes an independent choice from options. Example: each of merged intervals independently picks one of groups.
- Use combinations (): when you pick a specific number of items from a larger pool and order doesn't matter. Example: "how many ways can you choose exactly 3 intervals out of the total to form a single group?"
- Use permutations (): when you arrange items in a specific sequence. Example: "in what order can these intervals be processed?"
Worked example: 3 intervals, 2 groups
Every interval independently picks group A or B. The choice tree doubles at each level, so the leaf count is .
Change the question and the formula changes with it: "choose exactly 2 of the 3 intervals for group A" is (the leaves with exactly two A's), and "in what order are the 3 intervals processed" is .
Factorials
Trailing zeroes in factorial
Digit Counting & Sums
Count Digits in a Number
Easy·
4 Approaches
Digit-Extraction Loop
O(log N)O(1)
String Length
O(log N)O(log N)
Recursive Division
O(log N)O(log N)
Logarithm Formula
O(1)O(1)
Count Commas in Range
258. Add Digits
1837. Sum of Digits in Base K
3483. Unique 3-Digit Even Numbers
Easy·
Frequency Counting
O(1)O(1)
FIG. UNIQUE 3 DIGIT EVEN NUMBERS● INTERACTIVE
visualization loads as you reach it
Base Conversion
Decimal to binary
Easy·
4 Approaches
Digit-by-Digit Rebuild
O(log N)O(log N)
Bitwise Extraction
O(log N)O(log N)
Head Recursion
O((log N)^2)O(log N)
Built-in `bin()`
O(log N)O(log N)
504. Base 7
405. Convert a Number to Hexadecimal
3602. Hexadecimal and Hexatrigesimal Conversion
171. Excel Sheet Column Number
168. Excel Sheet Column Title
Number-to-Text Encoding
12. Integer to Roman
Medium·
3 Approaches
Greedy Value-Symbol Table
O(1)O(1)
Place-Value Lookup Tables
O(1)O(1)
Nested Threshold Logic
O(1)O(1)
13. Roman to Integer
273. Integer to English Words
Reversal & Palindromes
7. Reverse Integer
9. Palindrome Number
Power Checks
326. Power of Three
Easy·
4 Approaches
Repeated Division
O(log N)O(1)
Precomputed Powers Lookup
O(1)O(1)
Logarithm Identity
O(1)O(1)
Max Power Divisor
O(1)O(1)
1780. Check if Number is a Sum of Powers of Three
Medium·
3 Approaches
Base-3 Digit Check
O(log N)O(1)
Greedy Largest-Power Subtraction
O(15 log N)O(15)
Greedy Descending Power
O(2 log N)O(1)