Try it first — give it 15 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
This is a Codeforces problem. There's no function to fill in. Your program reads the input itself and prints the answer.
The input is one integer w (1 ≤ w ≤ 100), the weight of a watermelon. Two friends want to cut it into two pieces so that each piece weighs an even number, and each piece must weigh more than 0. The pieces don't have to be equal. Print YES if it's possible, otherwise NO.
Example: w = 8 → YES. You can cut it as 2 + 6 or 4 + 4.
Start with brute force
Try every cut. The first piece weighs a, from 1 to w − 1, and the second weighs w − a. If both are even, print YES.
With w ≤ 100 that's at most 99 checks, so this is perfectly fast. It's also a great way to discover the shortcut: run it in your head for small w and look at which ones say YES.
Building the intuition
Fill in a small table:
| w | Possible cuts | Any both-even? |
|---|---|---|
| 2 | 1 + 1 | No |
| 3 | 1 + 2, 2 + 1 | No |
| 4 | 2 + 2 | Yes |
| 5 | 1 + 4, 2 + 3, … | No |
| 6 | 2 + 4 | Yes |
Two patterns jump out:
- Odd weights never work. Two even numbers always add up to an even number, so they can't make an odd
w. - Even weights work, except 2. For any even
wof 4 or more, cut off 2. The rest,w − 2, is even and positive. Butw = 2can only become 1 + 1.
So the whole problem is one condition: w is even and w > 2.
The optimal approach
- Read
w. - If
w % 2 == 0andw > 2, printYES. Otherwise printNO.
Reading the input
The input is a single number on one line. Here is how each language reads it:
w = int(sys.stdin.readline()) # readline gives the text "8\n"; int() turns it into 8int w;
cin >> w; // cin skips spaces and newlines for youBufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int w = Integer.parseInt(in.readLine().trim()); // trim() drops stray spacesconst w = Number(require("fs").readFileSync(0, "utf8").trim()); // read everything, then convertInput always arrives as text. Converting it to a number is your job.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
import sys
def main():
w = int(sys.stdin.readline()) # input arrives as text: convert it to a number
# Even + even is always even, and the smallest split is 2 + 2,
# so w must be even AND bigger than 2
if w % 2 == 0 and w > 2:
print("YES")
else:
print("NO")
main()#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); // fast I/O: a good habit on Codeforces
cin.tie(nullptr);
int w;
cin >> w;
// Even + even is always even, and the smallest split is 2 + 2,
// so w must be even AND bigger than 2
if (w % 2 == 0 && w > 2) {
cout << "YES" << '\n';
} else {
cout << "NO" << '\n';
}
return 0;
}import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int w = Integer.parseInt(in.readLine().trim()); // input arrives as text: convert it
// Even + even is always even, and the smallest split is 2 + 2,
// so w must be even AND bigger than 2
if (w % 2 == 0 && w > 2) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}// Codeforces: choose "Node.js" as the language
const w = Number(require("fs").readFileSync(0, "utf8").trim()); // text -> number
// Even + even is always even, and the smallest split is 2 + 2,
// so w must be even AND bigger than 2
if (w % 2 === 0 && w > 2) {
console.log("YES");
} else {
console.log("NO");
}Complexity
- Time: O(1). One remainder and one comparison.
- Space: O(1).
Edge cases and traps
w = 2. The trap that catches most first submissions. It's even, but 1 + 1 isn't.w = 1. Odd, soNO.- Forgetting to convert the input. In Python and JavaScript the input is a string.
"8" % 2doesn't do what you want. - Printing the wrong text. Print exactly
YESorNO. Extra words likeAnswer: YESfail.
Say it in the interview
"Two even numbers always sum to an even number, so an odd weight can't work. Every even weight from 4 up works, by cutting off a piece of 2. The only even failure is 2 itself, because it splits only into 1 and 1. So I print YES exactly when w is even and greater than 2. That's constant time."
Follow-ups interviewers ask
- "What if it had to be split into k even parts?" Each part is at least 2, so you need
weven andw ≥ 2k. - "What if both parts had to be odd?" Odd + odd is even, so
wmust be even and at least 1 + 1 = 2.
Practise the same idea
This is Day 5 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free