Skip to content
  • Home
  • Contact Us
  • About Us
  • Privacy Policy
  • DMCA
  • Linkedin
  • Pinterest
  • Facebook
thecscience

TheCScience

TheCScience is a blog that publishes daily tutorials and guides on engineering subjects and everything that related to computer science and technology

  • Home
  • Human values
  • Microprocessor
  • Digital communication
  • Linux
  • outsystems guide
  • Toggle search form
HackerRank Number Line Jumps Problem Solution

HackerRank Number Line Jumps Problem Solution

Posted on April 11, 2023April 11, 2023 By Yashwant Parihar No Comments on HackerRank Number Line Jumps Problem Solution

In this post, We are going to solve HackerRank Number Line Jumps Problem.

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 x1 and moves at a rate of v1 meters per jump.
  • The second kangaroo starts at location x1 and moves at a rate of v1 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

x1 = 2

v1 = 1

x2 = 1

v2 = 2

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

Input Format

A single line of four space-separated integers denoting the respective values of x1, v1, x2, and v2.

Sample Input 0

0 3 4 2

Sample Output 0

YES

Explanation 0

The two kangaroos jump through the following sequence of locations:

From the image, it is clear that the kangaroos meet at the same location (number  on the number line) after the 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. x2 > x1, ). Because the second kangaroo moves at a faster rate (meaning v2 > v1) and is already ahead of the first kangaroo, the first kangaroo will never be able to catch up. Thus, we print NO.

HackerRank Number Line Jumps Problem Solution
HackerRank Number Line Jumps Problem Solution

Table of Contents

  • Number Line Jumps C Solution
  • Number Line Jumps C++ Solution
  • Number Line Jumps C Sharp Solution
  • Number Line Jumps Java Solution
  • Number Line Jumps JavaScript Solution
  • Number Line Jumps Python Solution

Number Line Jumps C Solution

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int x1; 
    int v1; 
    int x2; 
    int v2; 
    int ispossible = 0;
    scanf("%d %d %d %d",&x1,&v1,&x2,&v2);
    if ( v1 == v2 ) {
       if ( x1 == x2 ) {
           ispossible = 1;
       }
    } else {
        float it = (float)(x1-x2)/(v2-v1);
        if ( it >= 0 && (int)it == it ) {
           ispossible = 1;
        }
    }
    if ( ispossible == 1 ) {
        printf("YES");
    } else {
        printf("NO");
    }
    return 0;
}

Number Line Jumps C++ Solution

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

bool HasSolution(int x1, int x2, int v1, int v2)
{
    if (v1 <= v2) return false;
    
    return (x2-x1)%((v2-v1)) == 0;
    
}

int main() {
    int x1, x2, v1, v2;
    
    cin >> x1 >> v1 >> x2 >> v2;
    
    if (HasSolution(x1,x2,v1,v2))
    {
        cout << "YES" << endl;
    } else
        {
        cout << "NO" << endl;
    }
    
    return 0;
}

Number Line Jumps C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static void Main(String[] args) {
        string[] tokens_x1 = Console.ReadLine().Split(' ');
        int x1 = Convert.ToInt32(tokens_x1[0]);
        int v1 = Convert.ToInt32(tokens_x1[1]);
        int x2 = Convert.ToInt32(tokens_x1[2]);
        int v2 = Convert.ToInt32(tokens_x1[3]);
        
        int pass = HopsUntilSeparating(x1, v1, x2, v2);
        if(x1 + pass*v1 == x2 + pass*v2)
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
    
    static int HopsUntilSeparating(int x1, int v1, int x2, int v2)
    {
        if(x2 > x1 && v2 >= v1)
            return -1;
        else if (x2 < x1 && v2 <= v1)
            return -1;
        else if (x1 == x2)
            return -1;
        else if (v1 == v2)
            return 0;
        else
            return (Math.Abs(x1-x2) / Math.Abs(v1-v2));
    }
}

Number Line Jumps Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'kangaroo' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts following parameters:
     *  1. INTEGER x1
     *  2. INTEGER v1
     *  3. INTEGER x2
     *  4. INTEGER v2
     */

    public static String kangaroo(int x1, int v1, int x2, int v2) {
        int loc1 = x1;
        int loc2 = x2;
        if (v1 <= v2) {
            return "NO";
        }
        while (loc1 < loc2) {
            loc1 += v1;
            loc2 += v2;
        }
        if (loc1 == loc2) {
            return "YES";
        }
        return "NO";

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        int x1 = Integer.parseInt(firstMultipleInput[0]);

        int v1 = Integer.parseInt(firstMultipleInput[1]);

        int x2 = Integer.parseInt(firstMultipleInput[2]);

        int v2 = Integer.parseInt(firstMultipleInput[3]);

        String result = Result.kangaroo(x1, v1, x2, v2);

        bufferedWriter.write(result);
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Number Line Jumps JavaScript Solution

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var x1_temp = readLine().split(' ');
    var x1 = parseInt(x1_temp[0]);
    var v1 = parseInt(x1_temp[1]);
    var x2 = parseInt(x1_temp[2]);
    var v2 = parseInt(x1_temp[3]);
    console.log(myFunc(x1,v1,x2,v2));
    function myFunc(x1,v1,x2,v2){
  if(x1 == x2  && v1 == v2){//if speed and distance is equal
    return 'YES';
  }
  else if(x1 == x2 && v1 > v2){//if initial distance is same but car1 speed > car2's
    return 'NO';
  }
  else if(x1 == x2 && v1 < v2){//if initial distance is same but car1 speed < car2's
    return 'NO';
  }
  else if(x1 < x2 && v1 == v2){//if speed is same but car1 is behind car2
    return 'NO';
  }
  else if(x1 > x2 && v1 == v2){//if speed is same but car1 is ahead of car2
    return 'NO';
  }
  else if((x1 < x2 && v1 < v2) || (x2 < x1 && v2 < v1)){//if distance and speed of each car is less than that of other one
    return 'NO';
  }
  else {
    if((x2-x1) % (v2-v1) === 0){
      return 'YES';
    }
    else {
      return 'NO';
    }
  }
}
    
}

Number Line Jumps Python Solution

#!/bin/python3

import sys

x1,v1,x2,v2 = input().strip().split(' ')
x1,v1,x2,v2 = [int(x1),int(v1),int(x2),int(v2)]

if v1 <= v2 or ((x2 - x1) % (v1 - v2) > 0):
    print('NO')
else:
    print('YES')

Other Solutions

  • HackerRank Between Two Sets Problem Solution
  • HackerRank Breaking the Records Solution
c, C#, C++, HackerRank Solutions, java, javascript, python Tags:C, cpp, CSharp, Hackerrank Solutions, java, javascript, python

Post navigation

Previous Post: HackerRank Apple and Orange Problem Solution
Next Post: HackerRank Between Two Sets Problem Solution

Related Posts

HackerRank Running Time of Algorithms Problem Solution HackerRank Running Time of Algorithms Solution c
HackerRank Two Two Problem Solution HackerRank Two Two Problem Solution c
HackerRank Bill Division Problem Solution HackerRank Bill Division Problem Solution c
HackerRank Diagonal Difference Problem Solution HackerRank Diagonal Difference Problem Solution c
HackerRank Palindromic Border Problem Solution HackerRank Palindromic Border Problem Solution c
HackerRank Red Knight's Shortest Path Problem Solution HackerRank Red Knight’s Shortest Path Solution c

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick Your Subject
Human Values

Copyright © 2023 TheCScience.

Powered by PressBook Grid Blogs theme