Pattern Recognition
The fastest competitive programmers don’t think harder—they recognize patterns faster. This chapter trains your pattern recognition to instantly spot which technique to apply.The Recognition Framework
The 3-Step Recognition Process
1
Read the Constraint
The constraint is the FIRST clue to the algorithm.
2
Identify the Question Type
What are they asking? Count, optimize, find, check?
3
Match the Pattern
Combine constraint + question type = algorithm pattern.
Constraint → Algorithm Map
The Golden Table
Question Type Patterns
Pattern 1: “Count the number of…”
Count subarrays with property
Technique: Prefix sum + hashmap
Count subsequences with property
Technique: DP (usually 1D or 2D)
Pattern 2: “Find the maximum/minimum…”
Pattern 3: “Check if possible…”
Can we achieve X?
Technique: Binary search on answer
Does path/assignment exist?
Technique: DFS/BFS or DP
Pattern 4: “Find the kth…”
Keyword → Algorithm Triggers
Instant Recognition Keywords
Visual Pattern Gallery
Array Patterns
Tree Patterns
Graph Patterns
Decision Trees for Common Problems
”Given an array…” Decision Tree
”Given a string…” Decision Tree
Pattern Recognition Drills
Drill 1: Speed Classification
Read each problem description and identify the pattern in under 10 seconds:Problem Set (Click to expand)
Problem Set (Click to expand)
P1: “Given an array of n integers, find the number of pairs (i,j) where i < j and a[i] + a[j] = k.”
- Pattern: Two Sum → Hashmap
- Pattern: Tree DP → DFS with return value
- Pattern: Interval covering → Sort by end + greedy
- Pattern: Palindrome → DP or expand from center
- Pattern: Negative cycle → Bellman-Ford
- Pattern: Range min query → Sparse table or segment tree
- Pattern: 0/1 Knapsack → 2D DP
- Pattern: Inversions → Merge sort or BIT
Drill 2: Constraint → Algorithm
Answers
Answers