Skip to main content

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. Pattern Recognition Flow

The Recognition Framework

The Secret: After solving 500+ problems, you stop “solving” and start “recognizing.” This chapter gives you shortcuts to that recognition.

The 3-Step Recognition Process

3-Step Recognition
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

Constraint to Algorithm Map

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…”

Optimization Patterns

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

Keyword Triggers

Instant Recognition Keywords


Array Patterns

Array Pattern Recognition

Tree Patterns

Graph Patterns


Decision Trees for Common Problems

”Given an array…” Decision Tree

Array Problem 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:
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
P2: “Given a binary tree, find the maximum path sum from any node to any node.”
  • Pattern: Tree DP → DFS with return value
P3: “Given n intervals, find the minimum number of points such that each interval contains at least one point.”
  • Pattern: Interval covering → Sort by end + greedy
P4: “Given a string, find the longest palindromic substring.”
  • Pattern: Palindrome → DP or expand from center
P5: “Given a weighted graph, find if negative cycle exists.”
  • Pattern: Negative cycle → Bellman-Ford
P6: “Given array, answer queries for minimum in range [l,r].”
  • Pattern: Range min query → Sparse table or segment tree
P7: “Given n items with weight and value, maximize value with capacity W.”
  • Pattern: 0/1 Knapsack → 2D DP
P8: “Given a sequence, count inversions (pairs where i < j but a[i] > a[j]).”
  • Pattern: Inversions → Merge sort or BIT

Drill 2: Constraint → Algorithm


Quick Reference Cheat Sheet

One-Liner Pattern Recognition


Pattern recognition is a skill that develops with practice.Every problem you solve adds to your pattern library. After 500 problems, you’ll look at a new problem and think “Oh, this is just [pattern] with a twist.”Keep solving. Keep recognizing. Keep winning! 🏆