'Dividing a watermelon into two parts so that each of the them have a even number of kilo

Hi there is this problem : One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.

Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.

Input:The first (and the only) input line contains integer number w (1 ≤ w ≤ 100) — the weight of the watermelon bought by the boys.

Output:Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case.

I've tried this code.

#include <stdio.h>

int main () {
    int w,i,b;

    i=w%b;
    printf("enter the weight");
    scanf("%d", &w);
    for (b=2;b<=10;b=b+2) {
        if (i==0 && i&2==0) {
            printf("YES");
        } else {
            printf("NO");
        }
    }
    return 0;
}

but its not showing any correct output . Can you tell me what am I missing here?It would be a great help

c


Solution 1:[1]

#include<bits/stdc++.h>  
using namespace std; 
int main() {
    int t;
    cin>>t;
    if(t==2)
        cout<<"NO"<<endl;
    else if(t%2==0)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
return 0; }

The explanation here is:

  • No odd number can be divided into two even parts --> odd - even = odd
  • Almost every even number can be divided into two even parts --> even - even = even
  • Edge case is: 2; as it can NOT be divided into two even parts, 1 is odd number, and can not be divided into 2 & 0 as one boy will not take part in addition to that 0 is not even number

Solution 2:[2]

I created a shorter and most likely easier to comprehend code. Try this:

int main() {
    int number;
    cin >> number;
if (number % 2==0 && number != 2){
std::cout << "YES";
} else {
    std::cout << "NO";
}
}

Solution 3:[3]

Continuing from the comment, w should be unsigned int as you cannot have a negative weight. Additionally, if the least significant bit is 1 the number is odd. Putting those together in a simple mellon splitting algorithm, you could do something like the following to determine if each can have an even weighted slice:

#include <stdio.h>

int main (int argc, char **argv) {

    int i, j, wt = 0;

    for (i = 1; i < argc && sscanf (argv[i], "%u", &wt) == 1; i++) {
        int sliced = 0;
        if (wt & 1) goto odd;
        for (j = 0; j < wt/2; j++) {
            if (!(((wt/2 - j) & 1) | ((wt/2 + j) & 1))) {
                printf (" %3u - Yes (one gets %u, other %u)\n", wt,
                        wt/2 - j, wt/2 + j);
                sliced = 1;
                break;
            }
        }
        odd:;
        if (!sliced) printf (" %3u - No\n", wt);
    }

    return 0;
}

Example Use/Output

$ for i in {1..20}; do ./bin/evenmellons $i; done
   1 - No
   2 - No
   3 - No
   4 - Yes (one gets 2, other 2)
   5 - No
   6 - Yes (one gets 2, other 4)
   7 - No
   8 - Yes (one gets 4, other 4)
   9 - No
  10 - Yes (one gets 4, other 6)
  11 - No
  12 - Yes (one gets 6, other 6)
  13 - No
  14 - Yes (one gets 6, other 8)
  15 - No
  16 - Yes (one gets 8, other 8)
  17 - No
  18 - Yes (one gets 8, other 10)
  19 - No
  20 - Yes (one gets 10, other 10)

To compute and see all the possible even weighted slice combinations the friends can share, you can simply omit the break. You can make it selectable at the compile stage by wrapping the break in its own definition. For example:

#ifndef SHOWALL
                break;
#endif

Compile as usual for normal behavior, compile with -DSHOWALL to compute and show all possible slice combinations.

Solution 4:[4]

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int weight;
    cin>>weight;
    string soln = weight>2 && weight % 2==0 ? "Yes" : "No";
    cout<<soln;
}

Here, You just need to check it can divide or not and also the divided part must contain even number...

Solution 5:[5]

My current attempt is:

#include <stdio.h>

int main () {
    int w,x,y;
    
    printf("Enter The Weight");
    scanf("%d" , &w) ;
    for(y=2;y<=w;y=y+2) {
    x=w-y;
        if(x%2==0) {
            for(x;x>=y;x=w-y) {
                y=y+2 ;
            printf("Yes,It can divide to %d", x);
            printf("and %d\n", y-2);    
            }
        }   else {
            printf("No");
        }
    }
    return 0;
}           

Solution 6:[6]

Here is my answer in Python 3 (Answer accepted by Codeforces)

w = int(input())
if w%2==0 and w!=2:
   print("YES")
else:
   print("NO")

Solution 7:[7]

If the melon can be divided into two even numbers 2x and 2y (x, y ? 1), then the sum is 2*(x+y). Thus any even number ? 4 can be divided into two even parts.

To check for an even number, you can simply use x%2 == 0.

any even number can be divided into two evens. Except 2, 2=1+1. Hence the condition.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int w;
    cin>>w;
    if(w>2 && w%2==0){
        cout<<"YES"<<endl;
    }
    else{
        cout<<"NO"<<endl;
    }
    return 0;
}

Solution 8:[8]

try this one.

#include<stdio.h>
int main(){
    int a,b,c;
    scanf("%d",&a);
    if(a<100 && a>=1){
        if(a%2==0 && a>3){
            b=a/2;
            if(b%2==0){
                printf("YES\n");
                printf("%d %d",b,b);
            }else{
                b=b+1;
                c=b-2;
                printf("YES\n");
                printf("%d %d",c,b);
           }
        }else{
        printf("NO");
    } else{
       printf("Invalid Input");
    }
 return 0;
 }

Solution 9:[9]

#include <iostream>

using namespace std;

int main() {
    int w;
  
    cin>>w;
    if (w>2 && w%2==0) {
        cout<<"YES";
    } else {
        cout<<"NO";
    }
    return 0;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yunnosch
Solution 2 Anonymousstriker38596
Solution 3
Solution 4 Shashwat SIngh
Solution 5 David C. Rankin
Solution 6
Solution 7
Solution 8 M Hamza Javed
Solution 9 Matt Ke