Documentation Index
Fetch the complete documentation index at: https://resources.devweekends.com/llms.txt
Use this file to discover all available pages before exploring further.
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
Constraint → Algorithm Map
The Golden Table
| Constraint (n) | Max Operations | Likely Algorithm |
|---|---|---|
| n ≤ 10 | 10! = 3.6M | Brute force, permutations, backtracking |
| n ≤ 20 | 2²⁰ = 1M | Bitmask DP, subset enumeration |
| n ≤ 100 | n³ = 1M | Floyd-Warshall, O(n³) DP |
| n ≤ 500 | n³ = 125M | O(n²) with optimization |
| n ≤ 5,000 | n² = 25M | O(n²) DP, all pairs |
| n ≤ 10⁵ | n log n | Sorting, binary search, segment tree |
| n ≤ 10⁶ | O(n) | Linear scan, two pointers, prefix sum |
| n ≤ 10⁹ | O(log n) | Binary search, math formula |
| n ≤ 10¹⁸ | O(1) or O(log n) | Math formula, matrix exponentiation |
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…”
| What to Optimize | First Try | If TLE, Try |
|---|---|---|
| Max sum subarray | Kadane’s O(n) | Already optimal |
| Min cost path | Dijkstra O(E log V) | 0-1 BFS if weights 0/1 |
| Max flow | Ford-Fulkerson | Dinic’s algorithm |
| Max matching | Hungarian O(n³) | Hopcroft-Karp O(E√V) |
| Min operations | BFS | Binary search on answer |
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…”
| Problem Type | Algorithm | Complexity |
|---|---|---|
| Kth smallest in array | nth_element / quickselect | O(n) |
| Kth smallest in sorted arrays | Binary search | O(log(min(m,n))) |
| Kth smallest in BST | In-order traversal | O(k) |
| Kth smallest sum | Priority queue | O(k log k) |
Keyword → Algorithm Triggers
Instant Recognition Keywords
| Keyword in Problem | Algorithm to Consider |
|---|---|
| ”subarray sum” | Prefix sum, sliding window, kadane |
| ”longest increasing” | DP, binary search + DP |
| ”shortest path” | BFS (unweighted), Dijkstra (weighted) |
| “number of ways” | DP, combinatorics |
| ”partition into k” | Binary search on answer |
| ”lexicographically smallest” | Greedy, trie |
| ”connected components” | DFS, DSU |
| ”minimum spanning tree” | Kruskal, Prim |
| ”all pairs” | Floyd-Warshall, n × Dijkstra |
| ”parentheses/brackets” | Stack |
| ”intervals/meetings” | Sort by end, greedy |
| ”palindrome” | Two pointers, DP, Manacher |
| ”permutation” | Factorial, backtracking |
| ”subset” | Bitmask, DP |
| ”modulo 10^9+7” | DP (count), use mod everywhere |
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
| Constraint | Your Answer |
|---|---|
| n ≤ 15 | ? |
| n ≤ 1000, find shortest path | ? |
| n ≤ 10⁵, range sum queries | ? |
| n ≤ 10⁶, single pass required | ? |
| n ≤ 10¹⁸, compute nth Fibonacci | ? |
Answers
Answers
| Constraint | Algorithm |
|---|---|
| n ≤ 15 | Bitmask DP, brute force |
| n ≤ 1000, find shortest path | Floyd-Warshall O(n³) or BFS/Dijkstra |
| n ≤ 10⁵, range sum queries | Prefix sum or Segment tree |
| n ≤ 10⁶, single pass required | O(n) linear scan, two pointers |
| n ≤ 10¹⁸, compute nth Fibonacci | Matrix exponentiation |