Jumping Bunnies – HackerRank Solution

In this post, we will solve Jumping Bunnies HackerRank Solution. This problem (Jumping Bunnies) is a part of HackerRank Functional Programming series.


Bunnies are very cute animals who likes to jump a lot. Every bunny has his own range of jump. Lets say there are N bunnies and ith (i ∈ [1, N]) bunny jumps ji units. Consider a 1-D plane, where initially bunnies are at 0. All of them starts jumping in forward direction.

For example, consider the case of kth bunny. Initially he is at 0. After first jump, he will be at point jk. After second, he will be at 2 x jk and so on. After mth jump, he will be at point m x jk.

Two bunnies can only meet each other when they are on the ground. When on the ground, a bunny can wait any amount of time. Being a social animal, all of them decide to meet at the next point where all of them will be on the ground. You have to find the nearest point where all the bunnies can meet.

For example, if there are N = 3 bunnies where j1 = 2j2 = 3j3 = 4. Nearest point where all bunnies can meet again is at 12. First bunny has to jump six times, for second it is 4 times and for third it is 3 times.

Help bunnies to find the nearest point where they can meet again.

Input Format

First line will contain an integer, N, representing the number of bunnies. Second line will contain N space separated integer, j1, j2, . . . jN, representing the jumping distance of them.

Output Format

Print the nearest location where all bunnies can meet again.


  • 2 <= N <= 10
  • 1 <= ji <= 106
  • For each test case it is guaranteed that solution will not exceed 2 x 1018.

Sample Input #00

2 3 4

Sample Output #00


Sample Input #01

1 3

Sample Output #01



Sample Case #00: This is the same example mentioned in the statement above.
Sample Case #01: First bunny has to jump 3 times to point 3, whereas second bunny has to jump only one time to go at point . Point 3 will serve as their meeting point.

Solution – Jumping Bunnies – HackerRank Solution


import java.util.Scanner

object Solution {
  def gcd(a: Long, b: Long): Long = if (b == 0) a else gcd(b, a % b)

  def lcm(a: Long, b: Long): Long = a / gcd(a, b) * b

  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)

    val t = sc.nextInt
    println((0 until t).map(_ => sc.nextLong).reduce(lcm))

Note: This problem (Jumping Bunnies) is generated by HackerRank 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. Required fields are marked *