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
- What Are A/B Problems? — On Codeforces, each contest has problems labeled A, B, C, D, etc. A is the easiest and B is second easiest. They're designed to be solvable by beginners in 15-30 minutes. Your first goal should be: solve A and B in every contest you enter!
- Common Pattern: Simple Math — Many A problems are just math. 'Is n divisible by 2?' 'What's the minimum number of coins?' 'Can you split n into two even numbers?' These require no fancy algorithms — just careful thinking and basic operations (+, -, *, /, %).
- Common Pattern: Simulation — Some problems say 'do this process step by step.' Just simulate it! If the problem says 'repeat until X happens,' write a while loop that does exactly what the problem describes. Don't overthink — sometimes the brute force IS the solution.
- Common Pattern: Greedy — Greedy problems ask you to make the best choice at each step. 'Always pick the biggest item' or 'always sort first.' If the problem feels like you should sort the data and then make choices from largest to smallest (or smallest to largest), it's probably greedy!
- Common Pattern: Counting and Frequency — Many B problems ask you to count occurrences. How many times does each letter appear? What's the most frequent number? Use an array or map to count frequencies, then answer the question based on those counts.
- Step-by-Step Problem Solving — 1) Read the problem completely. 2) Understand with examples. 3) Think of the approach (math? simulation? greedy?). 4) Consider edge cases. 5) Code it up. 6) Test with the given examples. 7) Think of corner cases. 8) Submit!
- Don't Overthink A Problems! — The biggest mistake beginners make is overcomplicating A problems. If your solution needs more than 20 lines of logic, you might be overthinking. A problems are meant to be simple. Step back and look for a simpler approach.
- Learn from Wrong Answers — Getting Wrong Answer (WA) is normal! Check: did you read the problem right? Did you handle all edge cases? Is your output format correct (YES vs Yes)? Try the examples again. Try edge cases like n=1, n=0, all same numbers, maximum values.
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. 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. 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. 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. 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. 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. 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
- Codeforces Problem 4A — Watermelon (Codeforces)
- How to Approach CP Problems (Errichto)
- Easiest Codeforces Problems (sorted by solved count) (Codeforces)