Algorithm


Problem Name: Algorithms - Number Line Jumps

Problem Link: https://www.hackerrank.com/challenges/kangaroo/problem?isFullScreen=true

In this HackerRank Functions in Algorithms - Java programming problem solution,

You are choreographing 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 𝑥1 and moves at a rate of 𝑣1 meters per jump.
  • The second kangaroo starts at location 𝑥2 and moves at a rate of 𝑣2 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.

Example

𝑥1 = 2
𝑣1 = 1
𝑥2 = 1
𝑣2 = 2

After one jump, they are both at 𝑥 = 3, (𝑥1 + 𝑣1 = 2 + 1, 𝑥2 + 𝑣2 = 1 + 2), so the answer is YES.

Function Description

Complete the function kangaroo in the editor below.

kangaroo has the following parameter(s):

  • int x1, int v1: starting position and jump distance for kangaroo 1
  • int x2, int v2: starting position and jump distance for kangaroo 2

Returns

  • string: either YES or NO

 

Constraints

0 ≤ 𝑥1 < 𝑥2 ≤ 10000
1 ≤ 𝑣1 ≤ 10000
1 ≤ 𝑣2 ≤ 10000

 

 

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int x1;
    int v1;
    int x2;
    int v2;
    cin >> x1 >> v1 >> x2 >> v2;
    // Corner case: Second kangaroo is at least as fast as first kangaroo 
    if(v2 >= v1) {
        cout<<"NO";
    } else if((x2 - x1) % ( v1- v2) == 0) {
        cout << "YES";
    } else {
        cout << "NO";
    }
    return 0;
}
Copy The Code & Try With Live Editor

#2 Code Example with Java Programming

Code - Java Programming


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    private static final Scanner sc = new Scanner(System.in);

    public static void main(String[] args)
    {
        int x1 = sc.nextInt();
         int v1 = sc.nextInt();
          int x2 = sc.nextInt();
           int v2 = sc.nextInt();
           int i = 0;
       for(i = 0; i  <  10000; i++)
       {
           x1 = x1 + v1;
           x2 = x2 + v2;
           if(x1 == x2){
           System.out.print("YES");
             System.exit(0);}
       }
       System.out.print("NO"); 
}
}
Copy The Code & Try With Live Editor

#3 Code Example with C# Programming

Code - C# Programming


using System;
using static System.Console;

class Solution
{
    static void Main(String[] args)
    {
        var tokens_x1 = Console.ReadLine().Split(' ');
        var x1 = Convert.ToInt32(tokens_x1[0]);
        var v1 = Convert.ToInt32(tokens_x1[1]);
        var x2 = Convert.ToInt32(tokens_x1[2]);
        var v2 = Convert.ToInt32(tokens_x1[3]);
        var result = kangaroo(x1, v1, x2, v2);
        WriteLine(result);
    }

    static string kangaroo(int x1, int v1, int x2, int v2)
    {
        var sameLocationPossible = "";
        if (x1  <  x2 && v1 < v2)
            sameLocationPossible = "NO";

        else if (x2 < x1 && v2 < v1)
            sameLocationPossible = "NO";

        else if (x2  <  x1)
        {
            //v2 > v1
            var numberOfJumps = ((double)(x1 - x2)) / (v2 - v1);
            //check whether number of jumps is a whole number  i.e no fractional part.
            if (numberOfJumps % 1 == 0)
                sameLocationPossible = "YES";
            else
                sameLocationPossible = "NO";
        }
        else
        {
            //v1 > v2
            var numberOfJumps = ((double)(x2 - x1)) / (v1 - v2);
            //check whether number of jumps is a whole number  i.e no fractional part.
            if (numberOfJumps % 1 == 0)
                sameLocationPossible = "YES";
            else
                sameLocationPossible = "NO";
        }
        return sameLocationPossible;
    }
}
Copy The Code & Try With Live Editor

#4 Code Example with Python Programming

Code - Python Programming


import math
import os
import random
import re
import sys

# Complete the kangaroo function below.
def kangaroo(x1, v1, x2, v2):
    for n in range(10000):
        if((x1 + v1) == (x2 + v2)):
            return "YES"
        x1 += v1
        x2 += v2
    return "NO"

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    x1V1X2V2 = input().split()

    x1 = int(x1V1X2V2[0])

    v1 = int(x1V1X2V2[1])

    x2 = int(x1V1X2V2[2])

    v2 = int(x1V1X2V2[3])

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

    fptr.write(result + '\n')

    fptr.close()
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Apple and Orange in C, C++,Java, JavaScript, Python, PHP & C# solution in Hackerrank
Next
[Solved] Between Two Sets in C++,Java, Python, PHP & C# solution in Hackerrank