Math & Computation
Math problems test algorithmic thinking, number theory, and computational techniques. These problems often have elegant solutions using mathematical properties and efficient algorithms.Core Concepts
Key Areas
- Arithmetic Operations: Addition, multiplication, exponentiation
- Number Theory: Prime numbers, GCD, modular arithmetic
- Combinatorics: Permutations, combinations, counting
- Computational Geometry: Points, lines, areas
- Bit Manipulation: XOR, shifts, masks
Common Techniques
- Fast Exponentiation: O(log n) instead of O(n)
- String Arithmetic: Handle large numbers as strings
- Digit Manipulation: Process numbers digit by digit
- Mathematical Properties: Leverage patterns and formulas
Key Patterns & Techniques
1. Fast Exponentiation
- Compute x^n in O(log n) time
- Binary exponentiation (repeated squaring)
- Handle negative exponents
2. String Arithmetic
- Add/multiply large numbers as strings
- Digit-by-digit processing with carry
- Handle leading zeros
3. Digit Manipulation
- Extract digits: n % 10, n // 10
- Reverse number, find max digit
- Swap digits for optimization
4. Mathematical Optimization
- Greedy digit swapping
- Use properties (divisibility, parity)
- Avoid brute force with math insight
Common Approaches
Power(x, n) - Fast Exponentiation
Iterative Fast Exponentiation
Add Strings (Large Numbers)
Maximum Swap
Add Two Integers (Bit Manipulation)
Multiply Strings
Problems in This Category
021. Pow(x, n)
Medium | Frequency: 74.9%Fast exponentiation using binary exponentiation in O(log n).
050. Add Strings
Easy | Frequency: 51.9%Digit-by-digit addition with carry handling.
072. Maximum Swap
Medium | Frequency: 40.7%Greedy digit swapping to maximize number value.
085. Add Two Integers
Easy | Frequency: 32.0%Bit manipulation using XOR for sum and AND for carry.
Complexity Characteristics
| Pattern | Time Complexity | Space Complexity | When to Use |
|---|---|---|---|
| Fast Exponentiation | O(log n) | O(1) iterative | Power computation |
| String Arithmetic | O(max(m,n)) | O(max(m,n)) | Large numbers beyond int |
| Digit Manipulation | O(d) | O(1) or O(d) | d = number of digits |
| GCD/LCM | O(log(min(a,b))) | O(1) | Divisibility, fractions |
| Sieve of Eratosthenes | O(n log log n) | O(n) | Generate primes up to n |
Interview Tips
Pattern Recognition
- “x^n” or “power” → Fast exponentiation O(log n)
- “Large numbers” or “overflow” → String arithmetic
- “Maximize/minimize digit swap” → Greedy digit manipulation
- “Without +/-/*” → Bit manipulation
- “GCD/LCM” → Euclidean algorithm
- “Prime numbers” → Sieve or trial division
Common Mistakes
- Not handling negative exponents in power
- Forgetting leading zeros in string arithmetic
- Integer overflow in multiplication
- Not reversing result in string addition
- Off-by-one errors in digit indexing
- Forgetting carry in final iteration
Key Insights
- Fast exponentiation reduces O(n) to O(log n) using binary representation
- String arithmetic handles arbitrarily large numbers
- Greedy works for digit swapping optimization
- Bit manipulation can replace arithmetic operations
- Mathematical properties often lead to elegant solutions
- Carry handling is crucial in string arithmetic
Mathematical Algorithms
GCD (Euclidean Algorithm)
Check Prime
Sieve of Eratosthenes
Factorial with Modulo
Advanced Patterns
Fast Modular Exponentiation
Roman to Integer
Count Primes (Sieve)
Reverse Integer
Happy Number
Bit Manipulation Techniques
Common Operations
XOR Properties
Pro Tip: For power operations, always use fast exponentiation (binary exponentiation) to achieve O(log n) instead of O(n). For large numbers, use string arithmetic to avoid overflow. Many math problems have elegant solutions using mathematical properties - look for patterns before implementing brute force.