# Jumping on the Clouds: Revisited – HackerRank Solution

In this post, we will solve Jumping on the Clouds: Revisited HackerRank Solution. This problem (Jumping on the Clouds: Revisited) is a part of HackerRank Algorithms series.

A child is playing a cloud hopping game. In this game, there are sequentially numbered clouds that can be thunderheads or cumulus clouds. The character must jump from cloud to cloud until it reaches the start again.

There is an array of clouds,Â cÂ and an energy levelÂ e = 100. The character starts fromÂ c[0]Â and usesÂ 1Â unit of energy to make a jump of sizeÂ kÂ to cloudÂ c[(i + k) % n]. If it lands on a thundercloud,Â c[i] = 1, its energy (e) decreases byÂ 2Â additional units. The game ends when the character lands back on cloudÂ 0.

Given the values ofÂ n,Â k, and the configuration of the clouds as an arrayÂ c, determine the final value ofÂ eÂ after the game ends.

Example.Â c = [0, 0, 1, 0]
k = 2

The indices of the path areÂ 0 -> 2 -> 0. The energy level reduces byÂ 1Â for each jump toÂ 98. The character landed on one thunderhead at an additional cost ofÂ 2Â energy units. The final energy level isÂ 96.

Note:Â Recall thatÂ %Â refers to theÂ modulo operation. In this case, it serves to make the route circular. If the character is atÂ c[n – 1]Â and jumpsÂ 1, it will arrive atÂ c[0].

Function Description

Complete the jumpingOnClouds function in the editor below.

jumpingOnClouds has the following parameter(s):

• int c[n]: the cloud types along the path
• int k: the length of one jump

Returns

• int:Â the energy level remaining.

## Input Format

The first line contains two space-separated integers,Â nÂ andÂ k, the number of clouds and the jump distance.
The second line containsÂ nÂ space-separated integersÂ c[i]Â whereÂ 0 <= i < n. Each cloud is described as follows:

• IfÂ c[i] = 0, then cloudÂ iÂ is aÂ cumulusÂ cloud.
• IfÂ c[i] = 1, then cloudÂ iÂ is aÂ thunderhead.

## Constraints

• 2 <= n <= 25
• 1 <= k <= n
• n % k = 0
• c[i] = {0, 1}

Sample Input

``````STDIN             Function
-----             --------
8 2               n = 8, k = 2
0 0 1 0 0 1 1 0   c = [0, 0, 1, 0, 0, 1, 1, 0]``````

Sample Output

``92``

Explanation

Observe that our thunderheads are the clouds numberedÂ 2,Â 5, andÂ 6. The character makes the following sequence of moves:

1. Move:Â 0 -> 2, Energy:Â e = 100 – 1 – 2 = 97.
2. Move:Â 2 -> 4, Energy:Â e = 97 – 1 = 96.
3. Move:Â 4 -> 6, Energy:Â e = 96 – 1 – 2 = 93.
4. Move:Â 6 -> 0, Energy:Â e = 93 – 1 = 92.

## Solution – Jumping on the Clouds: Revisited – HackerRank Solution

### C++

```#include <bits/stdc++.h>

#define FO(i,a,b) for (int i = (a); i < (b); i++)
#define sz(v) int(v.size())

using namespace std;

int n, k, e;
int t[105];

int main() {
scanf("%d %d", &n, &k); e = 100;
FO(i,0,n) scanf("%d", t+i);
int i = 0;
while (1) {
e -= 2*t[i]+1;
i = (i+k)%n;
if (i == 0) break;
}
printf("%d\n", e);
}
```

### Python

```#!/bin/python3

import sys

def jumpingOnClouds(c, k):
cur = k % n
energy = 100 - 1 - c[cur]*2

while cur != 0:
cur = (cur + k) % n
energy -= 1 + c[cur]*2

return energy

if __name__ == "__main__":
n, k = input().strip().split(' ')
n, k = [int(n), int(k)]
c = list(map(int, input().strip().split(' ')))
result = jumpingOnClouds(c, k)
print(result)
```

### Java

```import java.util.*;
public class A
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

int n = in.nextInt();
int k = in.nextInt();

int[] array = new int[n];
for(int x = 0; x < n; x++)
{
array[x] = in.nextInt();
}

int cloud = 0;
int e = 100;
for(int y = 0; y < n; y++)
{
cloud = (cloud + k) % n;
e--;

if(array[cloud] == 1)
{
e -= 2;
}

if(cloud == 0)
{
break;
}
}

System.out.println(e);
}
}
```

Note: This problem (Jumping on the Clouds: Revisited) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.