Divide Them All - MOON Code Chef Easy Question
Divide Them All - Moon
Hello Everyone , today we will be discussing an easy question code named MOON.
Here is the Link to the PROBLEM
So the question is pretty straight forward , you just need to divide m candies and n truffles such that both the elves get equal sweetness .
Each truffle has sweetness Y and candy has sweetness X .
First total sweetness should be divisible by 2 .
Next step is to iteratively get the solution of i*x + j*y = s/2 && the same solution to satisfy (n-i)*x + (m-j)*y =s/2;
If we found the solution , voila , we got the solution , print yes ,
else print no
Please look at the below code and understand it . For any doubts , please leave in the comment section
Code :-
#include <iostream>
using namespace std;
int main() {
int t;
scanf("%d",&t);
while(t--)
{
int n,m,x,y;
cin>>n>>m>>x>>y;
long int s;
s=n*x+m*y;
if(s%2==0)
{
s=s/2;
int i=0;
int j=0;
int flag=1;
for(i=0;i<=n;i++)
{
flag=1;
for(j=0;j<=m;j++)
{
if(i*x+j*y==s && (n-i)*x+(m-j)*y==s)
{
flag=0;
cout<<"YES"<<endl;
break;
}
}
if(flag==0)
{break;}
}
if(flag==1)
{
cout<<"NO"<<endl;
}
}
else
{
cout<<"NO"<<endl;
}
}
// your code goes here
return 0;
}
Please Subscribe and follow my blog , and leave your comments in the comment section .
Hello Everyone , today we will be discussing an easy question code named MOON.
Here is the Link to the PROBLEM
So the question is pretty straight forward , you just need to divide m candies and n truffles such that both the elves get equal sweetness .
Each truffle has sweetness Y and candy has sweetness X .
First total sweetness should be divisible by 2 .
Next step is to iteratively get the solution of i*x + j*y = s/2 && the same solution to satisfy (n-i)*x + (m-j)*y =s/2;
If we found the solution , voila , we got the solution , print yes ,
else print no
Please look at the below code and understand it . For any doubts , please leave in the comment section
Code :-
#include <iostream>
using namespace std;
int main() {
int t;
scanf("%d",&t);
while(t--)
{
int n,m,x,y;
cin>>n>>m>>x>>y;
long int s;
s=n*x+m*y;
if(s%2==0)
{
s=s/2;
int i=0;
int j=0;
int flag=1;
for(i=0;i<=n;i++)
{
flag=1;
for(j=0;j<=m;j++)
{
if(i*x+j*y==s && (n-i)*x+(m-j)*y==s)
{
flag=0;
cout<<"YES"<<endl;
break;
}
}
if(flag==0)
{break;}
}
if(flag==1)
{
cout<<"NO"<<endl;
}
}
else
{
cout<<"NO"<<endl;
}
}
// your code goes here
return 0;
}
Please Subscribe and follow my blog , and leave your comments in the comment section .
Comments
Post a Comment