Goal-based paths · 12 steps
Competitive programming
Read constraints, pick the algorithm that fits them, and know the techniques contests rely on: bits, math, prefix sums, binary search on answer, graphs and DP.
- Who it is for
- Programmers starting contests who are comfortable with a language and basic data structures.
- By the end
- You can map input limits to a target complexity and reach for the matching technique quickly and correctly.
Step 1: From constraints to complexity
Contest limits tell you the intended complexity; recognising √n, n log n and harmonic loops is the first skill.
- ModuleM03 Big O· beginner
- Complexity exerciseInner loop stepping by the outer index· Θ(n log n)
- Complexity exerciseLoop until i² exceeds n· Θ(√n)
- Complexity exerciseA linear loop around a doubling loop· Θ(n log n)
Step 2: Bits and number theory
Overflow, modular arithmetic, gcd and bit tricks appear in a large share of easy and medium tasks.
- ModuleM33 Bit Manipulation· intermediate
- ModuleM34 Math· intermediate
- PatternBit tricks and arithmetic invariants· O(n) scans; O(set bits) popcount; O(log min(a, b)) gcd
- ProblemThe largest tile that fits both walls· beginner · O(log min(a, b)) iterations
- ProblemCount the 1 bits· beginner · O(number of 1 bits) ≤ O(width)
- ProblemThe sock without a pair· beginner · O(n)
- ProblemThe one ticket that never came back· beginner · O(n)
Step 3: Sorting and binary search on answer
"Minimise the maximum" and "smallest feasible value" tasks are binary searches over a monotonic check.
- ModuleM13 Sorting· intermediate
- ModuleM17 Binary Search Patterns· intermediate
- PatternBinary search on a monotonic predicate· O(log R × cost of ok)
- ProblemFind the first broken build· beginner · O(log n) checks
- ProblemWhere does a value start and end?· intermediate · O(log n)
- Complexity exerciseSort once, then binary search n times· Θ(n log n)
Step 4: Prefix sums and difference arrays
Range queries and range updates on static arrays are prefix sums and difference arrays.
- ModuleM16 Prefix Sum and Difference Array· intermediate
- PatternPrefix sums· O(n) build, O(1) per query
- ProblemHow many stretches add up to k?· intermediate · O(n) expected
- ProblemEvery product but your own· intermediate · O(n)
Step 5: Two pointers and windows
Linear-time scans over sorted data and contiguous ranges replace many quadratic ideas.
- ModuleM14 Two Pointers· intermediate
- ModuleM15 Sliding Window· intermediate
- PatternTwo pointers· O(n)
- PatternSliding window· O(n)
- ProblemTwo walls that hold the most water· intermediate · O(n)
- ProblemThe longest stretch without a repeated letter· intermediate · O(n) expected
Step 6: Monotonic stacks and deques
Nearest greater element and window maximum in O(n) are standard contest building blocks.
- ModuleM08 Stacks· beginner
- ModuleM09 Queues and Deques· beginner
- PatternMonotonic stack· O(n)
- ProblemHow many days until it gets warmer?· intermediate · O(n)
Step 7: Heaps
Needed for Dijkstra, greedy scheduling and k-way merges in the following steps.
- ModuleM21 Heaps and Priority Queues· intermediate
- PatternTop K with a heap· O(n log k)
- Collectionstd::priority_queue<T>· C++
Step 8: Graphs
Traversal, ordering, connectivity, shortest paths and spanning trees together cover most graph tasks.
- ModuleM23 Graph Fundamentals· intermediate
- ModuleM24 BFS and DFS· intermediate
- ModuleM25 Topological Sort· intermediate
- ModuleM28 Union Find· intermediate
- ModuleM26 Shortest Paths· advanced
- ModuleM27 Minimum Spanning Trees· advanced
- PatternBreadth-first search and level order· O(V + E)
- PatternTopological ordering· O(V + E)
- PatternUnion-find for connectivity· O(α(n)) amortized per operation
Step 9: Greedy
Greedy solutions are short but need a proof; exchange arguments and counterexamples are the contest skill.
- ModuleM30 Greedy· advanced
- PatternGreedy choice after sorting· O(n log n)
- PatternSort and merge intervals· O(n log n)
- ProblemBuy once, sell once, profit most· beginner · O(n)
Step 10: Backtracking
Small limits (n ≤ 20) signal exhaustive search with pruning, and backtracking leads into DP.
- ModuleM31 Backtracking· intermediate
- PatternDepth-first search and backtracking· O(V + E) for graph DFS; O(2ⁿ) subsets, O(n!) permutations
- Complexity exerciseGenerating every subset· Θ(n · 2ⁿ) including copying; 2ⁿ outputs
- Complexity exerciseGenerating every permutation· Θ(n · n!) with output
Step 11: Dynamic programming
The largest topic in contests; start with 1D states and space optimisation.
- ModuleM32 Dynamic Programming· advanced
- PatternDynamic programming (1D and 2D)· O(states × transitions)
- ProblemHow many ways up the stairs?· beginner · O(n)
- ProblemCollect the most without taking neighbours· intermediate · O(n)
- ProblemThe best streak of daily profits· intermediate · O(n)
Step 12: Combining techniques
Harder tasks combine two or three of the above; the final module practises that.