Friday, 19 January 2018

Kangaroo Hacker Rank Solution in Java

You are choreograhing a circus show with various animals. 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 as part of the show.
Complete the function kangaroo which takes starting location and speed of both kangaroos as input, and return  or  appropriately. Can you determine if the kangaroos will ever land at the same location at the same time? The two kangaroos must land at the same location after making the same number of jumps.
Input Format
A single line of four space-separated integers denoting the respective values of , and .
Constraints
Output Format
Print YES if they can land on the same location at the same time; otherwise, print NO.
Note: The two kangaroos must land at the same location after making the same number of jumps.
Sample Input 0
0 3 4 2
Sample Output 0
YES
Explanation 0
The two kangaroos jump through the following sequence of locations:
image
From the image, it is clear that the kangaroos meet at the same location (number  on the number line) after same number of jumps ( jumps), and we print YES.
Sample Input 1
0 2 5 3
Sample Output 1
NO
Explanation 1
The second kangaroo has a starting location that is ahead (further to the right) of the first kangaroo's starting location (i.e., ). Because the second kangaroo moves at a faster rate (meaning and is already ahead of the first kangaroo, the first kangaroo will never be able to catch up. Thus, we print NO.
Java Code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    
static String meetornot(int low,int lows,int high,int highs) {
    while(low != high)
    {
        low = low+lows;
        high = high+highs;
        if(low>high)
        {
            break;
        }
    }
    if(low==high)
    {
        return "YES";
    }
    else
    {
        return "NO";
    }
}
    static String kangaroo(int x1, int v1, int x2, int v2) {
        // Complete this function
        if((x2>x1 && v2>v1)||(x1>x2 && v1>v2)){return "NO";}
        else
        {
            if(x1>x2)
            {
                String res = meetornot(x2,v2,x1,v1);
                return res;
            }
            else
            {
                 String res = meetornot(x1,v1,x2,v2);
                return res;
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x1 = in.nextInt();
        int v1 = in.nextInt();
        int x2 = in.nextInt();
        int v2 = in.nextInt();
        String result = kangaroo(x1, v1, x2, v2);
        System.out.println(result);
    }
}




No comments:

Post a Comment

LCM of n numbers Using Java

Found it in Geeks For Geeks : https://www.geeksforgeeks.org/lcm-of-given-array-elements/ A good Solution to be Considered: // Java Pro...