Day 5 of 90CF 800Codeforces

Watermelon

Decide whether a watermelon of weight w can be cut into two parts that both weigh a positive even number.

Pattern
Parity check with one trap
Time box
15 min
Time
O(1)
Space
O(1)

Try it first — give it 15 minutes

0/4 revealed

Stuck? 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:

    wPossible cutsAny both-even?
    21 + 1No
    31 + 2, 2 + 1No
    42 + 2Yes
    51 + 4, 2 + 3, …No
    62 + 4Yes

    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 w of 4 or more, cut off 2. The rest, w − 2, is even and positive. But w = 2 can only become 1 + 1.

    So the whole problem is one condition: w is even and w > 2.

    The optimal approach

    1. Read w.
    2. If w % 2 == 0 and w > 2, print YES. Otherwise print NO.

    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 8
    int w;
    cin >> w;                        // cin skips spaces and newlines for you
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int w = Integer.parseInt(in.readLine().trim());  // trim() drops stray spaces
    const w = Number(require("fs").readFileSync(0, "utf8").trim());  // read everything, then convert

    Input 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()

    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, so NO.
    • Forgetting to convert the input. In Python and JavaScript the input is a string. "8" % 2 doesn't do what you want.
    • Printing the wrong text. Print exactly YES or NO. Extra words like Answer: YES fail.

    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 w even and w ≥ 2k.
    • "What if both parts had to be odd?" Odd + odd is even, so w must 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