Practice Makes Champion — Your CP Toolkit
You've built an arsenal. Now let's master when to use each weapon!
Open interactive version (quiz + challenge)Real-world analogy
You've just become a superhero who's collected 45 incredible gadgets on your journey! You've got a speed boost (sorting), a magnifying glass (binary search), a crystal ball (dynamic programming), a Swiss army knife (greedy), a map (graphs), and so many more. But here's the thing — a superhero who has every gadget but doesn't know which one to grab for each villain is just carrying a heavy backpack! This lesson is your field guide. It tells you: 'When you face THIS type of problem, reach for THAT tool.' Now you'll fight smarter, not harder.
What is it?
This is your graduation lesson — a complete recap of everything you've learned across 45 lessons, organized as a practical field guide. It maps problem types to the techniques you should reach for, gives you a practice roadmap, and sets you up for continued growth as a competitive programmer.
Real-world relevance
The problem-solving skills you've learned in competitive programming transfer directly to real-world software engineering. Tech companies like Google, Meta, Amazon, and Microsoft use CP-style problems in their interviews. The algorithmic thinking, debugging skills, and pattern recognition you've built are valued everywhere in the tech industry.
Key points
- The Toolkit You've Built — Over 45 lessons, you've learned an incredible range of techniques: arrays, sorting, binary search, recursion, greedy algorithms, dynamic programming, graphs (BFS, DFS, shortest path), number theory, bit manipulation, two pointers, sliding window, prefix sums, stacks, queues, trees, and more. That's a serious toolkit! Most competitive programmers spend years building up this knowledge, and you've got it all in one place.
- Pattern: 'Find If Something Exists' → Binary Search or Hashing — When a problem asks you to find a specific value, check if something is possible, or search in sorted data — think binary search! If you need to look up items quickly or count occurrences, hash maps (unordered_map) are your go-to. These are among the fastest lookup tools you have.
- Pattern: 'Find the Minimum/Maximum' → Greedy or DP — When a problem asks for the best, cheapest, shortest, or most efficient solution — start by asking: 'Can I make locally optimal choices?' If yes, go greedy. If choices affect each other and you need to consider all possibilities, use dynamic programming. Greedy is simpler and faster, so try it first!
- Pattern: 'Count the Number of Ways' → DP or Combinatorics — When a problem asks 'how many ways can you…?', think combinatorics (nCr, factorials, multiplication principle) for structured counting, or dynamic programming for counting paths through complex states. Many grid path-counting problems and subset-counting problems fall into this category.
- Pattern: 'Shortest Path / Connected Components' → Graph Algorithms — When a problem involves connections, networks, roads, or relationships — model it as a graph! Use BFS for shortest path in unweighted graphs, Dijkstra for weighted graphs, DFS for exploring and finding connected components. If the graph is a tree, even simpler techniques often work.
- Pattern: 'Process a Range / Subarray' → Prefix Sums, Sliding Window, or Two Pointers — When a problem involves contiguous subarrays or ranges — prefix sums let you compute any range sum in O(1). Sliding window is perfect when you're looking for the best subarray of fixed or variable size. Two pointers work when you need to find pairs or shrink/grow a window from both ends.
- Your Practice Roadmap — Here's your path forward: Start with Codeforces Div 2 A and B problems — these use basic implementation, sorting, greedy, and simple math. Move to C problems — these need binary search, prefix sums, two pointers, or simple DP. Then tackle D problems — these combine multiple techniques or need advanced DP and graph algorithms. Aim to solve 3-5 problems per week consistently.
- Online Judge Tips — Always read the constraints first — they tell you the expected time complexity. N ≤ 10^3 means O(N²) is fine. N ≤ 10^5 means you need O(N log N). N ≤ 10^7 means O(N) only. Watch out for integer overflow — use long long when numbers can exceed 2 billion. Test edge cases: N=0, N=1, all same elements, already sorted, reverse sorted.
- The Champion Mindset — The best competitive programmers aren't the ones who know the most algorithms — they're the ones who practice consistently and learn from every problem they can't solve. When you get stuck, don't just look at the solution — understand WHY that approach works. Upsolve problems after contests. Keep a notebook of techniques. Every problem you solve makes you faster and sharper for the next one!
Code example
#include <bits/stdc++.h>
using namespace std;
// === CP Contest Starter Template ===
// Tip: save this as template.cpp and use it for every contest!
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
const int MOD = 1e9 + 7;
void solve() {
int n;
cin >> n;
// Your solution goes here!
// Step 1: Read input
// Step 2: Identify the pattern (see lesson notes!)
// Step 3: Apply the right technique
// Step 4: Output the answer
vi a(n);
FOR(i, 0, n) cin >> a[i];
// Example: find max element
cout << *max_element(all(a)) << "\n";
}
int main() {
// Fast I/O — ALWAYS include this!
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t; // Uncomment for multiple test cases
while (t--) solve();
return 0;
}Line-by-line walkthrough
- 1. We start with our standard #include and using namespace std — every CP solution begins this way.
- 2. We define helpful shortcuts: 'll' for long long, 'pii' for pair of ints, 'vi' for vector of ints. These save typing during contests.
- 3. We define macros like 'pb' for push_back, 'all(x)' for begin/end, 'sz' for size, and 'FOR' for a shorter for-loop. Speed matters in contests!
- 4. MOD is set to 10^9 + 7, the most common modulo value in CP problems. Define it once and use it everywhere.
- 5. The solve() function handles one test case. This structure makes it easy to handle multiple test cases — just uncomment the cin >> t line.
- 6. Inside solve(), we read n and an array. The comments remind you of the problem-solving steps: read, identify pattern, apply technique, output.
- 7. As a simple example, we find and print the maximum element using max_element from the standard library.
- 8. In main(), the fast I/O lines (ios_base::sync_with_stdio(false) and cin.tie(NULL)) are crucial — they can make your solution 5-10x faster!
- 9. The test case loop (while t-- solve()) is the standard pattern. For single test case problems, t stays at 1.
- 10. Save this template and use it as your starting point for every contest problem. Customize it as you develop your own style!
Spot the bug
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
// Find the sum of all elements
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
}
cout << "Sum = " << sum << endl;
return 0;
}Need a hint?
This code looks perfectly fine for small numbers. But what if n = 200,000 and each element is 1,000,000,000? What happens to the variable 'sum'? Check the data type!
Show answer
The bug is integer overflow! If we have 200,000 elements each equal to 1,000,000,000, the sum would be 2 × 10^14, which far exceeds the int range (~2.1 × 10^9). The fix is to change 'int sum = 0' to 'long long sum = 0'. This is one of the most common bugs in competitive programming — always check if your answer could exceed 2 billion and use long long when it might!
Explain like I'm 5
Imagine you have a big toy box full of different toys you've collected on an amazing adventure — building blocks, magnifying glasses, magic wands, maps, and more! Now when your friend comes to you with a problem — like 'help me build the tallest tower!' — you know exactly which toy to grab. Building blocks! And if they say 'help me find the hidden treasure!' — you grab the map! This lesson is like putting labels on all your toys so you always know which one to pick. You've got all the toys now. Go play and have fun solving puzzles!
Fun fact
The word 'algorithm' comes from the name of the 9th-century Persian mathematician Al-Khwarizmi. And the youngest International Olympiad in Informatics (IOI) gold medalist was just 14 years old! Competitive programming has no age limit — some of the best programmers in the world started as kids, just like you.
Hands-on challenge
Create your own CP problem-solving cheat sheet! For each of these 10 problem types, write down which algorithm or technique you'd use and why: (1) Finding an element in a sorted array, (2) Shortest path in a maze, (3) Minimum coins to make change, (4) Number of ways to climb stairs, (5) Finding all prime factors, (6) Maximum subarray sum, (7) Checking if a string is a palindrome, (8) Finding connected groups in a network, (9) Scheduling non-overlapping events, (10) Counting pairs with a given sum. Then go to Codeforces and solve one problem for each type!
More resources
- Codeforces Problem Archive (Codeforces)
- Competitive Programming Roadmap (CP-Algorithms)
- AtCoder Beginner Contests (AtCoder)