← All posts

Watermelon, and why the answer is almost always yes

TLEvision / 6 Aug 2026

Problem 4A gives you a watermelon of weight www and asks whether two people can split it so that both parts are even and neither is zero.

The instinct is to loop over every split. You don't need to.

The shape of the answer

Write the split as w=a+bw = a + bw=a+b with a,b>0a, b > 0a,b>0 and both even. If both parts are even then their sum is even, so www must be even — an odd www is immediately no.

Now suppose www is even and w>2w > 2w>2. Take a=2a = 2a=2. Then b=w−2b = w - 2b=w−2, which is even because it's an even number minus an even number, and positive because w>2w > 2w>2. So a valid split always exists.

That leaves w=2w = 2w=2: the only split is 1+11 + 11+1, which is odd on both sides. No.

So the whole problem is

#include <iostream>

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

O(1)O(1)O(1) time, O(1)O(1)O(1) memory, and no loop anywhere.

The habit worth taking from it

The loop version passes too — w≤100w \le 100w≤100 in the original constraints. But the question "for which inputs is the answer no?" is faster to answer than "how do I search for a split?", and it generalises. When a problem asks does there exist, try characterising the failures first. There are usually very few.

Here there are exactly two: odd numbers, and 2.