Skip to content

Chapter 1: Essential Techniques

These are the techniques that solve most Div 2 A–C problems. Prefix sums, binary search, two pointers, greedy — these aren't fancy, but they're the workhorses you'll reach for hundreds of times. This chapter also teaches you how to think about problems: when to sort first, when to brute-force, and when a greedy approach works vs. when you need DP. Master these and you'll consistently solve 3–4 problems per contest.

Coming from: Chapter 0 — Fundamentals — C++ fluency, complexity analysis, and STL basics Going to: Chapter 2 — Data Structures — segment trees, DSU, and the data structures that power efficient algorithms Bridge: BRIDGE-to-data-structures.md — connecting techniques to the data structures that power them

Topics at a Glance

#TopicDifficultyKey Trigger Phrase
01How to Solve ProblemsBeginner"I don't know where to start"
02SimulationBeginner"Do exactly what the problem says"
03Sorting and SearchingBeginner"Sort first, then decide"
04Binary SearchBeginner"Sorted array" + lookup, or monotonic predicate
05Two PointersBeginner"Find pair with sum = X" in sorted array
06Sliding WindowBeginner"Longest/shortest subarray with property P"
07Prefix SumsBeginner"Range sum query" asked repeatedly
08Difference ArraysBeginner"Add +k to range [l, r]" many times
09Coordinate CompressionIntermediateHuge value range but few distinct values
10GreedyIntermediate"Maximize/minimize" + exchange argument works
11Complete SearchBeginnern ≤ 20 + "find best among all possibilities"
12Constructive AlgorithmsIntermediate"Build an object satisfying these constraints"
13Contribution TechniqueIntermediate"Sum over all pairs/subsets" → count each element's contribution
14Divide and ConquerBeginner"Split, recurse, combine" (merge sort pattern)
15Ternary SearchBeginner"Find min/max of unimodal function"
16Meet in the MiddleIntermediaten ≤ 40 + "enumerate subsets" (too big for 2ⁿ, split in half)
17Interactive ProblemsBeginner"Query the judge" + binary search on answer
18Randomization TechniquesIntermediate"Expected value trick" or "random sampling beats worst-case"

If You Only Read 3 Files

  1. Prefix Sums — because O(1) range sum queries are the single highest-value technique per line of code. You'll use this in nearly every contest.
  2. Binary Search — because "binary search on the answer" is a meta-technique that turns optimization problems into decision problems. Once you see it, it's everywhere.
  3. Greedy — because knowing when greedy works (and when it doesn't, so you need DP) is the most important judgment call in competitive programming.

Cross-Chapter Connections


File Listing

FileTopicLevel
how-to-solve-problemsHow to Solve Problems
simulationSimulation
sorting-and-searchingSorting and Searching
prefix-sumsPrefix Sums
difference-arraysDifference Arrays
two-pointersTwo Pointers
sliding-windowSliding Window
binary-searchBinary Search
ternary-searchTernary Search
complete-searchComplete Search and Backtracking
divide-and-conquerDivide and Conquer
interactive-problemsInteractive Problems
greedyGreedy Algorithms⭐⭐
constructive-algorithmsConstructive Algorithms⭐⭐
contribution-techniqueContribution Technique⭐⭐
coordinate-compressionCoordinate Compression⭐⭐
meet-in-the-middleMeet in the Middle⭐⭐
randomization-techniquesRandomization Techniques⭐⭐

Suggested Reading Order

  1. Simulation — implement what the problem says, step by step
  2. Sorting and Searching — sort first, then unlock many approaches
  3. Prefix Sums — O(1) range queries with the highest value per line of code
  4. Difference Arrays — O(1) range updates, the dual of prefix sums
  5. Two Pointers — turn O(n²) brute force into O(n)
  6. Sliding Window — maintain a moving window across input
  7. Binary Search — halve the search space every step
  8. Ternary Search — find extrema of unimodal functions
  9. Complete Search — systematically explore all candidates
  10. Divide and Conquer — split, recurse, combine
  11. Interactive Problems — query the judge in real time
  12. Greedy — locally optimal choices that yield global optima
  13. Constructive Algorithms — build objects satisfying constraints
  14. Contribution Technique — count each element's contribution
  15. Coordinate Compression — map sparse values to dense indices
  16. Meet in the Middle — reduce O(2^n) to O(2^(n/2))
  17. Randomization Techniques — probabilistic methods and hashing
  18. How to Solve Problems — the thinking framework from problem statement to correct code

Built for the climb from Codeforces 1100 to 2100.