Learn › Competitive programming intro

First problem walkthroughs

Three classic beginner judge problems, solved by reading the statement contract carefully. · 14 min read

Concept

Beginner CP problems often look tiny, but they teach the real workflow: translate the statement into a condition, identify the edge case, and print exactly what the judge expects. The following three are good first problems because each teaches a different habit.

Codeforces 4A — Watermelon

Codeforces 4A asks whether a watermelon of weight w can be split into two positive even parts. The key is that an even number can be written as even + even, but 2 is the trap: 2 = 0 + 2, and zero is not a positive part.

Watermelon condition
  1. w must be even — Odd total weight cannot be the sum of two even integers.
  2. w must be greater than 2 — Both parts must be positive, so 2 cannot split into two positive even pieces.
  3. answer = (w % 2 == 0 && w > 2) — This single condition covers the full input range.

Result: Print YES iff w is even and w > 2

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int w;
    cin >> w;
    cout << ((w % 2 == 0 && w > 2) ? "YES" : "NO") << '\n';
    return 0;
}

UVa 10055 — Hashmat the Brave Warrior

UVa 10055 is an input-until-EOF problem. Each line contains two army sizes; print their absolute difference. The important habits are: use long long, because values can be large, and loop while input succeeds.

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    long long a, b;
    while (cin >> a >> b) {
        cout << llabs(a - b) << '\n';
    }
    return 0;
}

UVa 100 — The 3n + 1 Problem

UVa 100 gives pairs i j. For every number between them, compute its Collatz cycle length, then print the original i j and the maximum cycle length in that inclusive range. The input order may be reversed, so preserve the original pair for output but iterate from min(i, j) to max(i, j).

Simulation contract
  1. n even -> n / 2 — The next Collatz value halves an even number.
  2. n odd -> 3n + 1 — Odd values jump upward before eventually falling.
  3. cycle length counts visited values including 1 — Start length at 1, then stop once n becomes 1.
  4. scan all n in [min(i,j), max(i,j)] — The beginner solution is direct simulation over the range.

Result: maximum cycle length over the inclusive interval

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

int cycleLength(long long n) {
    int len = 1;
    while (n != 1) {
        if (n % 2 == 0) n /= 2;
        else n = 3 * n + 1;
        len++;
    }
    return len;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int i, j;
    while (cin >> i >> j) {
        int lo = min(i, j);
        int hi = max(i, j);
        int best = 0;
        for (int n = lo; n <= hi; n++) {
            best = max(best, cycleLength(n));
        }
        cout << i << ' ' << j << ' ' << best << '\n';
    }
    return 0;
}

Blogs and resources

Keep a small resource shelf. You do not need to read everything at once; use these when a lesson or problem points you toward a specific topic.


Online judges to practice on

Pick one judge, make an account, and start with the easiest problems. The skill transfers — the platforms differ mostly in contest format and community.

  • Codeforces: the largest competitive site. Frequent rated rounds (Div. 1–4), an enormous problemset with editorials, a rating system, and the Edu section. Start at problem rating ~800 and climb.
  • AtCoder: clean, well-tested problems and beginner-friendly weekly ABC (Beginner) and ARC (Regular) contests. Excellent editorials; great for a structured ramp.
  • CodeChef: monthly Starters/long/cook-off contests and a large practice archive with difficulty ratings and a learning path.

Topic-specific practice sets live in their own chapters — e.g. the Dynamic programming chapter links the classic AtCoder EDPC set.

Previous: Reading statements and constraints · Next: Submitting, verdicts, and templates

More in Competitive programming intro