Number Line Jumps | HackerRank Solution

Hello coders, today we are going to solve Number Line Jumps HackerRank Solution which is a Part of HackerRank Algorithm Series.

Number Line Jumps



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 x2 and moves at a rate of v2 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.


x1 = 2
v1 = 1
x2 = 1
v2 = 2
After one jump, they are both at x = 3, (x1 + v1 = 2 + 1x2 + v2 = 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


  • string: either YES or NO

Input Format

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


  • 0 <= x1 < x2 < 10000
  • 1 <= v1 < 10000
  • 1 <= v2 <= 10000

Sample Input 0

0 3 4 2

Sample Output 0


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 12 on the number line) after same number of jumps (4 jumps), and we print YES.

Sample Input 1

0 2 5 3

Sample Output 1


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 > v1and is already ahead of the first kangaroo, the first kangaroo will never be able to catch up. Thus, we print NO.

Solution – Number Line Jumps


#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;
    if ((v1 <= v2) || ((x2 - x1) % (v2 - v1))) {
    } else {
    return 0;


import math
import os
import random
import re
import sys

# Complete the kangaroo function below.
def kangaroo(x1, v1, x2, v2):
    if x2 > x1 and v2 > v1:
        return "NO"
        if v2-v1 == 0:
            return 'NO'
            result = (x1-x2) % (v2-v1)
            if result == 0:
                return 'YES'
                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')



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

public class Solution {

    // Complete the kangaroo function below.
    static String kangaroo(int x1, int v1, int x2, int v2) {
        if (x1 == x2) {
            return "YES";
        int diff = v1 > v2 ? v1 - v2 : v2 - v1;
        if (diff == 0) {
            return "NO";
        int xdiff = x1 - x2;
        int vdiff = v2 - v1;
        if ((xdiff < 0 && vdiff < 0) || (xdiff > 0 && vdiff > 0)) {
            int mod = xdiff % vdiff;
            int mod2 = vdiff % xdiff;
            if (mod == 0 || mod2 == 0) {
                return "YES";
        return "NO";

    private static final Scanner scanner = new Scanner(;

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

        String[] x1V1X2V2 = scanner.nextLine().split(" ");

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

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

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

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

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




Disclaimer: The above Problem (Number Line Jumps) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.