Codeforces 4A: Watermelon Problem Solution & Explanation (C++)

Introduction

Welcome to the very first problem of our 50-Day Competitive Programming Challenge! If you are just starting your journey into competitive programming, the Watermelon problem (Codeforces 4A) is universally considered the best place to begin.

At first glance, this problem seems extremely simple, but it teaches a fundamental lesson that every programmer needs to learn: always look for the edge case. In this tutorial, we will break down the problem statement, analyze the underlying mathematical logic, and write a clean, beginner-friendly solution in C++.

The Problem Statement

Two friends, Pete and Billy, bought a watermelon weighing w kilos. They want to divide the watermelon into two parts such that each of the two parts weighs an even number of kilos. The parts do not need to be equal in weight.

Input: A single integer representing the total weight of the watermelon w (where 1 ≤ w ≤ 100).

Output: Print YES if the watermelon can be divided into two parts with even weights. Otherwise, print NO.

The Logic & Theory Breakdown

In competitive programming, problems are often wrapped in a story. Our first step is to strip away the fluff and look at the pure logic. The problem asks if a number w can be split into two positive, even integers.

Let’s look at the basic math:

  • An even number + an even number = an even number.
  • Therefore, the total weight w must be an even number. We can check this in C++ using the modulo operator (w % 2 == 0).

If a number is odd (like 5), it is impossible to split it into two even numbers (e.g., 2 + 3). So, odd numbers will always output NO.

The “Gotcha” (The Edge Case)

This is where beginners make their first mistake. They write if (w % 2 == 0) and submit their code, only to get a “Wrong Answer” error.

Why? We need to look at the smallest possible even number: 2. If the watermelon weighs 2 kilos, the only way to split it into two strictly positive parts is 1 kilo and 1 kilo. However, 1 is an odd number.

Therefore, 2 is the hidden edge case. The weight must be an even number, AND it must be strictly greater than 2.

C++ Code Implementation

Here is the clean, optimized C++ solution for the Watermelon problem.

#include <iostream>
using namespace std;

int main() {
    
    int w;
    cin >> w; // Read the weight of the watermelon

    // Condition 1: Total weight must be an even number (w % 2 == 0)
    // Condition 2: Total weight must be strictly greater than 2 (w > 2)
    if (w % 2 == 0 && w > 2) {
        cout << "YES\n";
    } else {
        cout << "NO\n";
    }

    return 0;
}

Complexity Analysis

  • Time Complexity: $O(1)$ – The algorithm only performs basic arithmetic checks and comparisons, which execute in constant time regardless of the input size.
  • Space Complexity: $O(1)$ – We are only using a single integer variable (w), meaning the memory required does not grow.

Key Takeaways

  • Deconstruct the Story: Always translate the word problem into a pure mathematical condition (in this case, checking for even numbers).
  • Hunt the Edge Case: Before submitting your code, always test the absolute minimum and maximum constraints. Testing w = 2 saves you from a failed submission.
  • Universal Logic: Even though we wrote this in C++, the logic (w % 2 == 0 && w > 2) remains exactly the same whether you are coding in Python, Java, or JavaScript!

Conclusion

Congratulations on solving your first major competitive programming challenge! You’ve learned how to read a problem, isolate the logic, and successfully navigate hidden edge cases.

If you found this helpful, be sure to check out the full video breakdown below where I whiteboard the entire logic step-by-step.

Hit the “Join” button on our YouTube channel to become a Snippet Supporter! You’ll get instant access to our downloadable ad-free Master Notes, featuring the complete code and step-by-step logic breakdown so you can confidently review before your placement drives.

Leave a Comment