Skip to main content

Time Complexity Analysis

Time Complexity Growth Comparison

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

Constraint Analysis Flowchart

The Constraint-to-Complexity Table

Memorize this table—it’s your first tool for problem-solving:
Quick Mental Check: Look at constraints FIRST. If n = 10^5 and you’re thinking of O(n^2), stop—you need a better approach.

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

O(n^2). The inner loop runs n + (n-1) + (n-2) + … + 1 = n(n+1)/2 times.

Pattern 2: Logarithmic Inner Loop

O(n log n). Outer loop: O(n). Inner loop: O(log n) since j doubles each time.

Pattern 3: Divisor Loop

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

Codeforces Gotcha: unordered_map can be hacked with adversarial inputs causing O(n) per operation. Use custom hash or stick with map in critical problems.

Master Theorem (Quick Reference)

For recursive algorithms: T(n) = aT(n/b) + f(n) Examples:
  • 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

  1. 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

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.