Can You Split a Watermelon Evenly? | Codeforces 4A Explained
A quick Python solution to one of the most iconic beginner problems on Codeforces
Here’s a simple but classic problem from Codeforces that’s perfect for beginners —
it tests your understanding of even numbers and edge conditions in just a few lines of code.
The challenge? Decide if a given number can be split into two positive even integers.
Try it yourself here:
Codeforces 4A – Watermelon
Problem breakdown
The goal is simple:
Given a watermelon of weight w, can we split it into two parts such that both parts are positive even integers?
Let’s clarify the requirements:
Both parts must be even numbers — no odd-number leftovers allowed.
Both must be greater than 0 — no empty slices.
They don’t have to be equal in weight.
For example:
8→YES(2+6,4+4, etc.)
6→YES(2+4)
3→NO(Can’t split into two even numbers)
The trick here is realizing that:
Any even number greater than 2 can be split into two positive even numbers.
So really, the only invalid even number is 2 — too small to divide into two positive even integers.
My python solution
def dividable(w):
for i in range(2,w,2):
if i%2==0 and (w-i)%2==0:
return "YES"
else:
continue
return "No"
if __name__ == "__main__":
w = int(input(""))
print(dividable(w))Here’s the first version of my solution:
This code loops through all even numbers from 2 up to w - 2, and checks whether both i and w - i are even.
If it finds such a pair, it immediately returns "YES".
If the loop ends without finding any valid combination, it returns "No".
It works — but after understanding the underlying logic, I realized it can actually be simplified quite a lot.
Cleaner Version
Once you know that any even number greater than 2 is splittable, the logic becomes crystal clear:
def dividable(w):
return "YES" if w > 2 and w % 2 == 0 else "No"This one-liner checks two conditions:
The weight
wmust be even (w % 2 == 0)It must be greater than 2 (because 2 is too small to split into two positive even numbers)
Time and Space Complexity
Time Complexity
First version: O(n) — due to the loop
Second version: O(1) — constant time check
Space Complexity
Both versions use O(1) space
Ham Tori’s Takeaway
Don’t let simple problems fool you — they’re often a great way to sharpen your thinking.
This one may seem easy at first glance, but it teaches an important skill:
how to spot patterns in constraints and reduce logic to its cleanest form.
See you in the next problem!

