#PROBLEM LINK:

https://www.codechef.com/TNP62121/problems/TNP604

*Author:* Setter’s Name

*Tester:* Tester’s Name

*Editorialist:* Editorialist’s name

DIFFICULTY : EASY

# PREREQUISITES: NIL

#PROBLEM:

There are two rabbits on an x-axis ready to jump in the positive direction (i.e, toward positive infinity). The first rabbit starts at location x1 and moves at a rate of v1 feet per jump. The second rabbit starts at location x2 and moves at a rate of v2 feet per jump. Given the starting locations and movement rates for each rabbit, can you determine if they’ll ever land at the same location at the same time?

Note : The two rabbits must land at the same location after making the same number of jumps.

#SOLUTION:

#include

#include<stdio.h>

using namespace std;

int main() {

int x1,x2,v1,v2,flag=0;

cin>>x1>>v1>>x2>>v2;

if(v1<=v2)

{

if(x1==x2&&v1==v2) cout<<“YES”<<endl;

else cout<<“NO”<<endl;

}

else

{

while(x1<=x2)

{

x1+=v1;

x2+=v2;

if(x1==x2){ cout<<“YES”;flag=1; break; }

}

if(!flag) cout<<“NO”<<endl;

}

return 0;

}

#EXPLANATION:

Example case 1 : If the Input is (0 3 4 2) ,then the two rabbits jump through the following sequence of locations:

0 -> 3 -> 6 -> 9 -> 12

4 -> 6 -> 8 ->10 -> 12

Thus, the rabbits meet after 4 jumps and we print YES.

Example case 2 : If the Input is (0 2 5 3) ,then the second rabbit has a starting location that is ahead (further to the right) of the first rabbits’s starting location (i.e., x2 > x1). Because the second rabbit moves at a faster rate (ie., v2 > v1 ) and is already ahead of the first rabbit, the first rabbit will never be able to catch up. Thus, we print NO.