Lesson 37 of 50 intermediate

Solving Easy Contest Problems (A/B Problems)

Your first real contest battles — let's win some problems!

Open interactive version (quiz + challenge)

Real-world analogy

Solving easy contest problems is like warming up before a big game. Athletes don't start with the hardest moves — they stretch, jog, and practice basic drills first. A/B problems are your warm-up! They build confidence, teach you patterns, and get your brain in contest mode. Every grandmaster started with these!

What is it?

This lesson teaches you how to approach and solve easy competitive programming problems (A and B problems on Codeforces). These are the foundation of competitive programming — mastering them builds your confidence and teaches you the core patterns (math, simulation, greedy, counting) that appear in harder problems too.

Real-world relevance

The step-by-step problem-solving approach works everywhere in life. Engineers break big projects into small solvable pieces. Doctors diagnose by eliminating possibilities one by one. Even cooking follows a process — read the recipe, gather ingredients, follow steps, taste and adjust. Systematic problem-solving is a universal life skill!

Key points

Code example

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

// Example A Problem: "Watermelon"
// Can you split weight w into two EVEN positive numbers?
void watermelon() {
    int w;
    cin >> w;
    // Two even numbers sum to an even number >= 4
    if (w >= 4 && w % 2 == 0)
        cout << "YES\n";
    else
        cout << "NO\n";
}

// Example B Problem: "Most Frequent Character"
// Given a string, find the most frequent character
void mostFrequent() {
    string s;
    cin >> s;
    int freq[26] = {0};  // Count each letter
    for (char c : s) freq[c - 'a']++;
    
    int maxFreq = 0;
    char result = 'a';
    for (int i = 0; i < 26; i++) {
        if (freq[i] > maxFreq) {
            maxFreq = freq[i];
            result = 'a' + i;
        }
    }
    cout << result << "\n";
}

// Example: Multiple test cases (very common!)
void solveMultiple() {
    int t;      // Number of test cases
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        // Solve for each test case
        // Example: Is n a perfect square?
        int sq = sqrt(n);
        if (sq * sq == n)
            cout << "YES\n";
        else
            cout << "NO\n";
    }
}

int main() {
    // Uncomment the function you want to test:
    // watermelon();
    // mostFrequent();
    solveMultiple();
    return 0;
}

Line-by-line walkthrough

  1. 1. The watermelon function: we read a weight w. To split it into two even positive numbers, both numbers must be at least 2, so the minimum sum is 4. Also, even + even = even, so w must be even. Therefore: w >= 4 AND w is even.
  2. 2. The mostFrequent function: we use a frequency array of size 26 (for each letter a-z). For each character, freq[c - 'a']++ counts it. Then we scan the array to find the highest count.
  3. 3. freq[c - 'a']++ — this is a classic trick! 'a' - 'a' = 0, 'b' - 'a' = 1, ..., 'z' - 'a' = 25. So each letter maps to an index 0-25.
  4. 4. The solveMultiple function shows the most common CP pattern: read t (number of test cases), then solve each one in a while(t--) loop.
  5. 5. int sq = sqrt(n); if (sq * sq == n) — to check if n is a perfect square, we take the square root, round it to an integer, and check if squaring it gives back n. Simple and effective!
  6. 6. Each function demonstrates a different common pattern: math (watermelon), counting (most frequent), and multiple test cases (perfect square check).

Spot the bug

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int a[n];
        for (int i = 0; i < n; i++) cin >> a[i];
        
        int max = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] > max) max = a[i];
        }
        cout << max << "\n";
    }
    return 0;
}
Need a hint?
What if all the numbers in the array are negative? What would 'max' be initialized to?
Show answer
The bug is initializing max to 0! If all numbers are negative (like -5, -3, -8), none of them are greater than 0, so max stays at 0 — which is wrong because 0 isn't even in the array! The fix is to initialize max to a very small number: int max = INT_MIN; or better yet, initialize it to a[0] (the first element) and start the loop from i=1. Also, 'max' shadows std::max — use 'maxVal' as the variable name to avoid issues.

Explain like I'm 5

Imagine you're solving puzzles at a carnival. The first booth has easy puzzles — 'what's 2 + 3?' The second booth is a tiny bit harder — 'what's the biggest number in this list?' You solve them one by one, getting prizes and confidence. The harder booths come later, but you don't need to rush! Start with the easy ones, learn the tricks, and work your way up. That's how you get good at contest problems!

Fun fact

The 'Watermelon' problem (Codeforces 4A) is one of the most solved problems in competitive programming history, with over 600,000 accepted solutions! But it STILL has more wrong answers than right ones — many people forget the edge case that w=2 should output 'NO' (you can't split 2 into two even positive numbers because the minimum is 2+2=4).

Hands-on challenge

Solve this problem: Given a number n, determine if it can be represented as the sum of exactly two positive EVEN numbers. For example, 8 = 2+6 (YES), 6 = 2+4 (YES), 3 (NO — no two even numbers sum to odd), 2 (NO — minimum is 2+2=4). Handle multiple test cases! Then go to Codeforces and try problem 4A (Watermelon) for real!

More resources

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