Lesson 36 of 50 intermediate

Reading Contest Problems Like a Pro

The most important CP skill that nobody teaches!

Open interactive version (quiz + challenge)

Real-world analogy

Reading a contest problem is like reading a recipe before cooking. If you skip a step or misread '1 tablespoon' as '1 cup' of salt, your dish is ruined! Similarly, misreading a problem means you'll write a correct solution... to the WRONG problem. Careful reading saves hours of debugging!

What is it?

Reading contest problems is the most underrated skill in competitive programming. It means carefully understanding what a problem is asking before you write a single line of code. This includes parsing the input/output format, studying examples, reading constraints, and identifying the core algorithmic challenge hidden behind the problem's story.

Real-world relevance

In software engineering, misunderstanding requirements is the #1 cause of project failures. Reading problems carefully in CP teaches you to be precise in real life. Lawyers read contracts word by word. Doctors read lab results carefully. Engineers double-check blueprints. The skill of careful reading transfers to every career!

Key points

Code example

#include <bits/stdc++.h>
using namespace std;

// Example: A typical CP problem template after reading the problem

// Problem: Given n numbers, find how many pairs have a sum equal to k.
// Input: First line: n and k. Second line: n integers.
// Output: Print the number of pairs.
// Constraints: 1 <= n <= 1000, 1 <= k <= 10^6

int main() {
    // Step 1: Read input (exactly as the format says)
    int n, k;
    cin >> n >> k;  // First line: n and k
    
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];  // Second line: n integers
    }
    
    // Step 2: Solve (n <= 1000, so O(n^2) is fine!)
    int count = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {  // j starts at i+1 to avoid counting pairs twice
            if (a[i] + a[j] == k) {
                count++;
            }
        }
    }
    
    // Step 3: Output (exactly as the format says)
    cout << count << "\n";
    
    return 0;
}

// READING CHECKLIST:
// [x] Read entire problem
// [x] Input format: n, k on first line; n numbers on second line
// [x] Output format: single number (count of pairs)
// [x] Constraints: n <= 1000 -> O(n^2) is OK
// [x] Examples verified by hand
// [x] Special cases: what if no pairs exist? -> print 0

Line-by-line walkthrough

  1. 1. The problem says 'First line: n and k' — so we read two numbers with cin >> n >> k. Getting the input format right is step one!
  2. 2. The problem says 'Second line: n integers' — so we create a vector of size n and read each number in a loop.
  3. 3. Constraints say n <= 1000. That means n^2 = 1,000,000 which is about 10^6 operations — well within the time limit (usually 10^8 operations per second). So a simple double loop is fine!
  4. 4. for (int j = i + 1; j < n; j++) — we start j at i+1, not 0, because: (a) we don't want to pair an element with itself, and (b) we don't want to count the pair (a[i], a[j]) twice (once as i,j and once as j,i).
  5. 5. if (a[i] + a[j] == k) count++; — this checks if the pair sums to k. Simple and correct!
  6. 6. cout << count << '\n'; — we print exactly what the problem asks: a single number. No extra words, no extra spaces. Just the answer.
  7. 7. The READING CHECKLIST at the bottom is a great habit! Before coding any CP problem, mentally check off: read the problem, understood input, understood output, checked constraints, verified with examples.

Spot the bug

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    int a[n];
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    int maxVal = 0;
    for (int i = 1; i <= n; i++) {
        if (a[i] > maxVal) maxVal = a[i];
    }
    cout << maxVal << "\n";
    return 0;
}
Need a hint?
Arrays in C++ start at index 0, not 1. If you declare int a[n], valid indices are 0 to n-1. What happens when you access a[n]?
Show answer
The bug is an off-by-one error in the array indexing! The array a[n] has valid indices 0 to n-1, but the loops use indices 1 to n. When i equals n, a[n] accesses memory OUTSIDE the array (undefined behavior), which can crash or give wrong answers. The fix is to change the loop to 'for (int i = 0; i < n; i++)'. This is one of the most common bugs in competitive programming — always double-check your loop bounds!

Explain like I'm 5

Imagine your teacher gives you a word problem in math: 'Sam has 5 apples. He gives 2 to Lucy and buys 3 more. How many does Sam have?' If you don't read carefully and just see 'apples...2...3,' you might answer 5. But the right answer is 5 - 2 + 3 = 6. You need to read every word! Contest problems are the same — every word matters, and skipping even one detail can give you the wrong answer.

Fun fact

In the 2019 ICPC World Finals, a top team lost because they misread 'non-decreasing' as 'increasing.' One word cost them the contest! Tourist (Gennady Korotkevich), the #1 competitive programmer in the world, has said that he spends about 30-50% of his time READING problems before coding. Reading carefully isn't slow — it's smart!

Hands-on challenge

Here's a practice problem: 'Given n integers, find the maximum difference between any two elements.' Before coding, answer these questions: (1) What is the input format? (2) What is the output? (3) What constraints would make O(n) necessary vs O(n^2) OK? (4) What's the simplest approach? Then code your solution. Hint: do you really need to check all pairs, or is there a shortcut using min and max?

More resources

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