 Federico Pintaluba
I'm a 27 years old developer, I have been coding for more than a decade, but I have focused in Android Development for the past 5 years. Here you will find all my projects, ideas, thoughts. Basically everything I made with <3.

# HackerRank: Jumping on the Clouds

### Problem

Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2. She must avoid the thunderheads. Determine the minimum number of jumps it will take Emma to jump from her starting postion to the last cloud. It is always possible to win the game.

For each game, Emma will get an array of clouds numbered 0 if they are safe or 1 if they must be avoided. For example, c = [0, 1, 0, 0, 0, 1, 0] indexed from 0…6. The number on each cloud is its index in the list so she must avoid the clouds at indexes 1 and 5. She could follow the following two paths: 0->2->4->6 or 0->2->3->4->6. The first path takes 3 jumps while the second takes 4.

Function Description

Complete the jumpingOnClouds function in the editor below. It should return the minimum number of jumps required, as an integer.

jumpingOnClouds has the following parameter(s):

• c: an array of binary integers

Input Format

The first line contains an integer n, the total number of clouds. The second line contains n space-separated binary integers describing clouds c[i] where 0 <= i < n.

Constraints

Output Format

Print the minimum number of jumps needed to win the game.

Sample Input 0

``````7
0 0 1 0 0 1 0
``````

Sample Output 0

``````4
``````

Explanation 0:
Emma must avoid c and c. She can win the game with a minimum of 4 jumps:

Sample Input 1

``````6
0 0 0 0 1 0
``````

Sample Output 1

``````3
``````

Explanation 1:
The only thundercloud to avoid is c. Emma can win the game in 3 jumps:

### Solution

Let’s start always by reading the rules, a pretty important one here for the proper solution is: She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2. She must avoid the thunderheads.

That’s why we have multiple solution, but only one is correct, the shortest one avoiding thunderheads.

So, let’s start by iterating over the clouds, and find consecutives 0 clouds. For that, let’s create a variable named accumulator, that will sum all 0 clouds that are one after another.

``````var accumulator = 0

for (cloud in c) {
if (cloud == 0) {
accumulator++
}
}``````

Now that we have the accumulator variable summing consecutive 0 clouds, and the maximum consecutive jumps Emma can make is 2, let’s check if we hit 2 in our variable, add a step and reset it to 0

``````    for (cloud in c) {
if (cloud == 0) {
accumulator++

if (accumulator == 2) {
steps++
accumulator = 0
}
}
}``````

Now, if we hit a 1 cloud iterating over our clouds list, we need to add a step and reset accumulator as we did when we hit 2, if we had this values outside the function, we could extract this into a separate function, but for this solution, we will end with something like this.

``````fun jumpingOnClouds(c: Array<Int>): Int {
var accumulator = 0
var steps = 0

for (cloud in c) {
if (cloud == 0) {
accumulator++

if (accumulator == 2) {
steps++
accumulator = 0
}
} else {
accumulator = 0
steps++
}
}

return steps
}``````

Let’s run the sample input in our local environment and see if we get the sample output.

``````7
0 0 1 0 0 1 0
``````

Running…

``4``

Bazinga!

Let’s submit the code in HackerRank and see if we pass all test cases.

Hope this helped you understand a fair solution made in Kotlin for this problem, if you have any suggestion or a better way of doing this in the same language, don’t hesistate on commenting below!

Share