Time Complexity Analysis
Why This Matters in Competitive Programming
In competitive programming, you have 2-3 seconds to process up to 10^6 or even 10^8 operations. Before writing a single line of code, you must estimate: “Will my solution pass?”The Golden Rule: A modern computer performs about 10^8 simple operations per second. If your algorithm exceeds this for the given constraints, you’ll get TLE (Time Limit Exceeded).
Pattern Recognition: Constraint Analysis
The Constraint-to-Complexity Table
Memorize this table—it’s your first tool for problem-solving:| Constraint (n) | Max Complexity | Typical Algorithms |
|---|---|---|
| n ≤ 10 | O(n!) | Brute force permutations |
| n ≤ 20 | O(2^n) | Bitmask DP, subset enumeration |
| n ≤ 100 | O(n^3) | Floyd-Warshall, 3 nested loops |
| n ≤ 1,000 | O(n^2) | Simple DP, nested loops |
| n ≤ 10^5 | O(n log n) | Sorting, binary search, segment trees |
| n ≤ 10^6 | O(n) | Two pointers, prefix sums, linear DP |
| n ≤ 10^8 | O(log n) or O(1) | Binary search on answer, math |
Step-by-Step Complexity Analysis
Step 1: Count the Loops
Step 2: Recognize Logarithmic Patterns
Step 3: Identify Amortized Complexity
Some algorithms look slow but are actually fast:Common Complexity Patterns
Pattern 1: Nested Loops with Dependency
Answer
Answer
O(n^2). The inner loop runs n + (n-1) + (n-2) + … + 1 = n(n+1)/2 times.
Pattern 2: Logarithmic Inner Loop
Answer
Answer
O(n log n). Outer loop: O(n). Inner loop: O(log n) since j doubles each time.
Pattern 3: Divisor Loop
Answer
Answer
O(n log n). This is the harmonic series: n/1 + n/2 + n/3 + … + n/n = n * H_n ≈ n log n.
The CP Complexity Checklist
Before submitting, ask yourself:1
Read Constraints
What are the input limits? (n, m, q, etc.)
2
Estimate Operations
Multiply your complexity by the largest input. Example: O(n^2) with n = 10^5 → 10^10 operations → TLE.
3
Consider Hidden Factors
Are you using
map (log n per operation) or unordered_map (O(1) amortized)?
Are you copying vectors/strings in loops?4
Check Recursive Depth
Stack overflow can occur with recursion depth > 10^6. Use iterative or increase stack size.
Common Pitfalls
Pitfall 1: String Concatenation
Pitfall 2: Passing by Value
Pitfall 3: Map vs Unordered_Map
Master Theorem (Quick Reference)
For recursive algorithms: T(n) = aT(n/b) + f(n)| Condition | Complexity |
|---|---|
| f(n) = O(n^c) where c < log_b(a) | O(n^(log_b(a))) |
| f(n) = O(n^c) where c = log_b(a) | O(n^c log n) |
| f(n) = O(n^c) where c > log_b(a) | O(f(n)) |
- Merge Sort: T(n) = 2T(n/2) + O(n) → O(n log n)
- Binary Search: T(n) = T(n/2) + O(1) → O(log n)
Practice Problems
Level 1: Constraint Analysis
- Given n ≤ 10^6, which complexities will pass in 2 seconds?
- O(n) ✓
- O(n log n) ✓
- O(n^2) ✗
Level 2: Analyze This Code
Answer
Answer
O(n log n). The outer loop runs log n times, inner loop runs n times each.
Key Takeaways
Constraint First
Always read constraints before thinking about the algorithm.
10^8 Rule
About 10^8 simple operations per second.
Hidden Costs
Watch for string copies, map operations, and recursive calls.
Practice Estimation
With experience, you’ll estimate complexity in seconds.
Next Up
Chapter 2: Fast I/O & Debugging
Learn input/output optimizations that can mean the difference between AC and TLE.