12 September 2016

Kangaroo

Standard

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

Input Format
A single line of four space-separated integers denoting the respective values of , and .
Constraints
  • 0<= x1 < x2 <= 10000
  • 1 <= x1 <= 10000
  • 1<= v2 <= 10000
Output Format
Print YES if they can land on the same location at the same time; otherwise, print NO.

MY EXPLANATION: 
This is a simple high school maths problem like 2 trains start at different location,given their velocity can you find whether they will ever meet or not?
Let's name the kangaroo's as K1 and K2 and given K2 starts ahead of K1 (i.e. x2 > x1).
Now, they will meet only when distance traveled by K1 is equal to the sum of distance traveled by K2 and their initial separation(i.e. (x1-x2) + distance(K2) ). This is visualized below.

let say they meet after time T then the condition will be
            v1*T = (x2 - x1) + v2*T
In other word, they will meet if
T = ( x2 - x1 ) % ( v1 - v2 )  is equal to 0 ( zero)
Now try to write your own code keeping these things in mind.

If you don't succeed than take a look at my solution.


#include <iostream>

using namespace std;

int main(){   

int x1, x2, v1, v2; /* Initializing variables */ 

cin >> x1 >> v1 >> x2 >> v2; /* assigining values to the variables  */ 

if( v1>v2  &&  (x2-x1)%(v1-v2)==0 )cout<<"YES"; /* if this is the case than they will meet , so print YES */ 

else cout<<"NO"; /* In any other case they wont meet */   

return 0;



Question Source: hackerrank.com 

7 comments:

  1. Hello..
    One of the most popular reasons as to why quick property sale services have become so popular in recent years comes from how this type of sale is one that can be handled on a variety of different types of properties. There are a number of properties that can be handled through the use of a quick property sale. This is a type of sale that can be used to help you with getting your property sold off quickly.One of the most valuable processes that you can use with regards to avoiding repossession is to look into getting into a quick property sale. This is a type of sale that can be used to help you with getting your property sold off quickly.property for sale

    ReplyDelete
  2. Nice. This was a nice explanation. Thanx.

    ReplyDelete
  3. If this week’s PGA Championship is anything like the other majors this season, it will be one of the most-watched PGA Championships in recent memory. Much of that can be attributed to the return of Tiger Woods.

    Whether it’s Woods, Rory McIlroy, Jordan Spieth or another reason that has you tuning in for the event, there will be no shortage on places your can view it. As far as online streaming goes, there will be featured group coverage provided by the event and Turner and CBS, which owns the rights to broadcast the tournament. (Look for Woods to be in one of those featured groups. He has been most of the season.)

    Check out the schedule below for all your streaming needs, which can be found on PGA.com and the PGA Championship mobile app. (All times ET.)

    Featured Group No. 1 (a.m. and p.m. groups)
    Thurs / Fri: 8:30 a.m. – 7 p.m.
    Sat / Sun: 11 a.m. – 7 p.m.

    Featured Group No. 2 (a.m. and p.m. groups)
    Thurs / Fri: 9:30 a.m. – 7 p.m.
    Sat / Sun: 11 a.m. – 7 p.m.

    Featured Holes: 16, 17, & 18
    Thurs/Fri: 2 p.m. – 8 p.m.
    Sat/Sun: 11 a.m. – 7 p.m.

    TNT Simulcast
    Thurs/Fri: 2 – 8 p.m.
    Sat/Sun: 11 a.m. – 2 p.m.

    CBS Simulcast
    Sat / Sun: 2 p.m. – 7 p.m.

    PGA Championship
    PGA Championship Live
    PGA Championship Live Stream
    PGA Championship 2018
    PGA Championship Golf

    PGA Championship
    PGA Championship Live
    PGA Championship 2018
    PGA Championship Golf
    2018 PGA Championship

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete