Lesson 48 of 50 beginner

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

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. 1. We start with our standard #include and using namespace std — every CP solution begins this way.
  2. 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. 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. 4. MOD is set to 10^9 + 7, the most common modulo value in CP problems. Define it once and use it everywhere.
  5. 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. 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. 7. As a simple example, we find and print the maximum element using max_element from the standard library.
  8. 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. 9. The test case loop (while t-- solve()) is the standard pattern. For single test case problems, t stays at 1.
  10. 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

Open interactive version (quiz + challenge) ← Back to course: CP Zero to Hero