# 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.

Contents

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 = 2,Â j2 = 3,Â j3 = 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.

## Constraints

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

Sample Input #00

``````3
2 3 4``````

Sample Output #00

``12``

Sample Input #01

``````2
1 3``````

Sample Output #01

``3``

Explanation

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

Scala

```import java.util.Scanner

object Solution {
@scala.annotation.tailrec
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.