For one act, you are given two kangaroos on a number line 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.
You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return YES, otherwise return NO.
For example, kangaroo  starts at  with a jump distance  and kangaroo  starts at  with a jump distance of . After one jump, they are both at , (), so our answer is YES.
Description
Complete the function kangaroo in the editor below. It should return YES if they reach the same position at the same time, or NO if they don't.
kangaroo has the following parameter(s):
  • x1, v1: integers, starting position and jump distance for kangaroo 1
  • x2, v2: integers, starting position and jump distance for kangaroo 2


Input 
A single line of four space-separated integers denoting the respective values of , and .
Output 
Print YES if they can land on the same location at the same time; otherwise, print NO.

Question from www.hackerrank.com


Solution in C# -



class Solution {

    static string kangaroo(int x1, int v1, int x2, int v2) {

        if(v1<=v2)
        return "NO";
        else
        {
            int sp = v2 - v1;
            int ds = x2 - x1;
            if(ds%sp==0)
            return "YES";
            else
            return "NO";
        }

    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        string[] x1V1X2V2 = Console.ReadLine().Split(' ');

        int x1 = Convert.ToInt32(x1V1X2V2[0]);

        int v1 = Convert.ToInt32(x1V1X2V2[1]);

        int x2 = Convert.ToInt32(x1V1X2V2[2]);

        int v2 = Convert.ToInt32(x1V1X2V2[3]);

        string result = kangaroo(x1, v1, x2, v2);

        textWriter.WriteLine(result);

        textWriter.Flush();
        textWriter.Close();
    }
}



Post a Comment

Previous Post Next Post