HackerRank Sales by Match Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Number Line Jumps solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank Number Line Jumps solution
HackerRank Number Line Jumps solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Number Line Jumps Solution

Task

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.

Example

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

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.

Constraints

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

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 12 line) after same number of jumps ( 4 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 solution

Number Line Jumps Solution in C

#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; 
    scanf("%d %d %d %d",&x1,&v1,&x2,&v2);
    if(x2>x1){
        if(v2<v1){
            if((x2-x1)%(v1-v2)==0)
                printf("YES");
            else
                printf("NO");
        }
        else
            printf("NO");
    }
    else{
        if(v1>v2){
            if((x1-x2)%(v2-v1)==0)
                printf("YES");
            else
                printf("NO");
        }
        else if(x1==x2&&v1==v2)
            printf("YES");
            else
            printf("NO");
    }
    return 0;
}

Number Line Jumps Solution in Cpp

#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))) {
        puts("NO");
    } else {
        puts("YES");
    }
    return 0;
}

Number Line Jumps Solution in Java

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

public class Solution {

    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();
        
        if (v1>v2&&(x2-x1)%(v1-v2)==0)
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}

Number Line Jumps Solution in Python

#!/bin/python

import sys


x1,v1,x2,v2 = raw_input().strip().split(' ')
x1,v1,x2,v2 = [int(x1),int(v1),int(x2),int(v2)]
if v1==v2:
    print 'NO'
else:
    if (x1-x2+v2-v1)%(v2-v1)==0:
        if (x1-x2+v2-v1)/(v2-v1)>0:
            print 'YES'
        else:
            print 'NO'
    else:
        print 'NO'

Number Line Jumps Solution using JavaScript

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]);

    var l1 = x1;
    var l2 = x2;
    
    for (var i=0; i< 10001; i++){
        l1+=v1;
        l2+=v2;
        
        if(l1 == l2){
            console.log("YES");
            return;
        }
    }
    console.log("NO");
}

Number Line Jumps Solution in Scala

object Solution {

    def main(args: Array[String]) {
        val sc = new java.util.Scanner (System.in);
        var x1 = sc.nextInt();
        var v1 = sc.nextInt();
        var x2 = sc.nextInt();
        var v2 = sc.nextInt();
        
        if (x1 == x2) println("YES")
        else if (x1 > x2 && v1 < v2 && ((x1 - x2) % (v2 - v1) == 0)) println("YES")
        else if (x1 < x2 && v1 > v2 && ((x2 - x1) % (v1 - v2) == 0)) println("YES")
        else println("NO")
    }
}

Number Line Jumps Solution in Pascal

var
   x1,x2,v1,v2:longint;


begin
   readln(x1,v1,x2,v2);
   if (v1>v2) AND ((x2-x1) mod (v1-v2)=0) then
     writeln('YES')
   else
     writeln('NO');
end. 

Disclaimer: This problem (Number Line Jumps) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Drawing Book Solution

Leave a Reply

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