How to Recognize Which Algorithm to Use? — A Pattern-Based Guide for Competitive Programming

Правка en1, от sharique02, 2026-07-08 08:06:15

"Knowing algorithms is important. Knowing when to use them is what separates beginners from strong problem solvers."

Hello everyone!

One of the most common questions beginners ask is:

"I know Binary Search, DFS, BFS, Sliding Window, DP... but how do I know which one to use?"

The answer is simple—but it takes practice.

Competitive programming is less about memorizing algorithms and more about recognizing patterns. Every problem leaves clues in its statement. Your job is to spot them.

In this blog, I'll share a practical framework that has helped me organize my thinking while solving problems.

Step 1: Read the Constraints First

Before thinking about the algorithm, check the input size.

Input Size Typical Complexity n ≤ 20 Brute Force / Bitmask / Backtracking n ≤ 1,000 O(n²) n ≤ 100,000 O(n log n) or O(n) n ≤ 1,000,000 O(n)

If n = 2 × 10⁵, an O(n²) solution is almost certainly too slow.

Constraints eliminate many wrong ideas before you write a single line of code.

Step 2: Look for Keywords

Certain words appear repeatedly in competitive programming problems.

Binary Search

Look for phrases like:

Minimum possible Maximum possible Smallest value Largest value Answer is monotonic

Example:

What is the minimum speed required to finish all bananas within H hours?

Think: Binary Search on Answer

Sliding Window

Common clues:

Subarray Contiguous Longest Shortest At most K Exactly K

Example:

Longest substring without repeating characters.

Think: Sliding Window + Hash Map

Two Pointers

Common clues:

Sorted array Pair Triplet Remove duplicates Merge

Example:

Find two numbers whose sum equals X.

Think: Two Pointers

Prefix Sum

Common clues:

Range sum Many queries Sum from L to R

Example:

Answer Q range sum queries efficiently.

Think: Prefix Sum

Difference Array

Common clues:

Range updates Add X to every element from L to R Multiple update operations

Think: Difference Array

Greedy

Typical phrases:

Minimum operations Maximum profit Earliest finish Optimal choice at every step

Before using a greedy algorithm, always ask:

Can I prove that choosing the locally best option always leads to a globally optimal solution?

If you cannot justify it, be cautious.

Dynamic Programming

Common clues:

Count the number of ways Maximum value Minimum cost Decisions at each step Overlapping subproblems

Whenever the same state appears multiple times, DP is often a good candidate.

Graph Algorithms

Keywords:

Cities Roads Network Flights Connected components Shortest path

Common choices:

BFS DFS Dijkstra Topological Sort Union-Find (DSU) Trees

Keywords:

Root Parent Child Subtree Lowest Common Ancestor

Expect:

DFS Tree DP Binary Lifting Euler Tour Step 3: Ask Yourself These Questions

Whenever you start a new problem, go through this checklist:

Is the array sorted?

→ Two Pointers or Binary Search.

Is the answer monotonic?

→ Binary Search.

Is the problem about contiguous subarrays?

→ Sliding Window or Prefix Sum.

Are there many range queries?

→ Prefix Sum, Segment Tree, or Fenwick Tree.

Are there many range updates?

→ Difference Array, Fenwick Tree, or Segment Tree.

Is the problem asking for shortest paths?

→ BFS or Dijkstra.

Is the same computation repeated?

→ Dynamic Programming.

A Simple Decision Tree Start │ ├── Is the array sorted? │ ├── Yes → Two Pointers / Binary Search │ └── No │ ├── Is it about contiguous subarrays? │ ├── Yes → Sliding Window / Prefix Sum │ └── No │ ├── Are there many queries? │ ├── Yes → Prefix Sum / Segment Tree │ └── No │ ├── Is it a graph? │ ├── Yes → BFS / DFS / Dijkstra │ └── No │ ├── Are states repeating? │ ├── Yes → Dynamic Programming │ └── Try Greedy or Constructive Thinking The Biggest Mistake Beginners Make

Many beginners try to match every problem with the last algorithm they learned.

For example:

Learned DP yesterday → Every problem looks like DP. Learned Segment Tree today → Every problem looks like Segment Tree.

Instead, let the problem guide the algorithm, not the other way around.

How to Improve Pattern Recognition

Pattern recognition is a skill built over time.

A routine that has worked well for many competitive programmers is:

Solve the problem. Read the editorial, even if you solved it. Compare your idea with the official approach. Write down the key observation. Revisit the problem after a few weeks.

Over time, you'll stop seeing isolated problems and start recognizing familiar patterns.

Practice Problems Binary Search Koko Eating Bananas Aggressive Cows Minimum Time to Complete Trips Sliding Window Longest Substring Without Repeating Characters Maximum Consecutive Ones III Minimum Size Subarray Sum Prefix Sum Range Sum Query Subarray Sum Equals K Two Pointers Two Sum II Container With Most Water 3Sum Dynamic Programming Climbing Stairs House Robber Coin Change Final Thoughts

Learning more algorithms is useful, but learning when to apply them is what truly improves your problem-solving ability.

The next time you open a problem, don't ask:

"Which algorithm do I know?"

Instead, ask:

"What is this problem trying to tell me?"

The constraints, keywords, and structure of the problem often point you toward the right solution.

I hope this guide helps beginners build intuition for choosing algorithms instead of relying on guesswork.

If you have additional pattern-recognition tips or think I missed an important category, feel free to share them in the comments. I'd love to learn from the community.

Happy Coding!

Теги tutorial, beginner, algorithms, competitive-programming

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский sharique02 2026-07-08 08:06:15 5928 Initial revision (published)