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

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

*2*For each game, Emma will get an array of clouds numbered ** 0** if they are safe or

**if they must be avoided. For example,**

*1***indexed from**

*c = [0, 1, 0, 0, 0, 1, 0]***. The number on each cloud is its index in the list so she must avoid the clouds at indexes**

*0…6***and**

*1***. She could follow the following two paths:**

*5***or**

*0->2->4->6***. The first path takes**

*0->2->3->4->6***jumps while the second takes**

*3***.**

*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

**space-separated binary integers describing clouds**

*n***where**

*c[i]***.**

*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[2]** and

**. She can win the game with a minimum of**

*c[*5]**jumps:**

*4***Sample Input 1**

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

**Sample Output 1**

```
3
```

**Explanation 1:**

The only thundercloud to avoid is ** c[4]**. Emma can win the game in

**jumps:**

*3*### 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!