--> The 'Number Line Jumps' - HackerRank Solution By Java

The Number Line Jumps – HackerRank Solution

Overview of Number Line Jumps

In this problem, you have two kangaroos on a number line. Each kangaroo starts at a different position and jumps forward by a fixed distance every time they jump. Your task is to determine whether the kangaroos will land in the same position simultaneously after making several jumps.

Number Line Jumps Problem Description

You need to determine if two kangaroos will land in the same position at the same time on a number line given their starting positions and jump distances.

Input

You will receive four integers:

  • x1: The starting position of the first kangaroo.
  • v1: The jump distance of the first kangaroo.
  • x2: The starting position of the second kangaroo.
  • v2: The jump distance of the second kangaroo.
Output

Print “YES” if the kangaroos will land in the same position at the same time. Otherwise, print “NO”.

Example 1
Input:

x1 = 0
v1 = 3
x2 = 4
v2 = 2

Output:

YES

Explanation:
  • Kangaroo 1 starts at position 0 and jumps 3 units each time.
  • Kangaroo 2 starts at position 4 and jumps 2 units each time.
  • After 1 jump:
    • Kangaroo 1 is at position 3 (0 + 3).
    • Kangaroo 2 is at position 6 (4 + 2).
  • After 2 jumps:
    • Kangaroo 1 is at position 6 (0 + 3 + 3).
    • Kangaroo 2 is at position 8 (4 + 2 + 2).
  • After 4 jumps, both kangaroos are at position 12 (0 + 43 = 12 and 4 + 42 = 12).

Since they meet at position 12 after 4 jumps, the output is “YES”.

Example 2
Input:

x1 = 0
v1 = 2
x2 = 5
v2 = 3

Output:

NO

Explanation:
  • Kangaroo 1 starts at position 0 and jumps 2 units each time.
  • Kangaroo 2 starts at position 5 and jumps 3 units each time.
  • After 1 jump:
    • Kangaroo 1 is at position 2 (0 + 2).
    • Kangaroo 2 is at position 8 (5 + 3).
  • After 2 jumps:
    • Kangaroo 1 is at position 4 (0 + 2 + 2).
    • Kangaroo 2 is at position 11 (5 + 3 + 3).

No matter how many jumps they make, they will never land in the same position simultaneously. Thus, the output is “NO”.

Example 3
Input:

x1 = 2
v1 = 1
x2 = 1
v2 = 2

Output:

YES

Explanation:
  • Kangaroo 1 starts at position 2 and jumps 1 unit each time.
  • Kangaroo 2 starts at position 1 and jumps 2 units each time.
  • After 1 jump:
    • Kangaroo 1 is at position 3 (2 + 1).
    • Kangaroo 2 is at position 3 (1 + 2).

Since they meet at position 3 after 1 jump, the output is “YES”.

number line jumps page 2
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) {//14 4 98 2
    // Write your code here
        int distance = Math.abs(x1-x2);
        for(int i=0; i<distance; i++){
            x1 += v1;
            x2 += v2;
            if(x1 == x2) 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();
    }
}
The Problem Solution

Leave a Reply

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