Hackerrank - Jumping on the Clouds: Revisited Solution
2 min read

Hackerrank - Jumping on the Clouds: Revisited Solution

Hackerrank - Jumping on the Clouds: Revisited Solution

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

To play, Aerith is given an array of clouds,  and an energy level . She starts from  and uses  unit of energy to make a jump of size  to cloud . If Aerith lands on a thundercloud, , her energy () decreases by  additional units. The game ends when Aerith lands back on cloud .

Given the values of , , and the configuration of the clouds as an array , can you determine the final value of  after the game ends?

For example, give  and , the indices of her path are . Her energy level reduces by  for each jump to . She landed on one thunderhead at an additional cost of  energy units. Her final energy level is .

Note: Recall that  refers to the modulo operation. In this case, it serves to make the route circular. If Aerith is at  and jumps , she will arrive at .

Function Description

Complete the jumpingOnClouds function in the editor below. It should return an integer representing the energy level remaining after the game.

jumpingOnClouds has the following parameter(s):

  • c: an array of integers representing cloud types
  • k: an integer representing the length of one jump

Input Format

The first line contains two space-separated integers,  and , the number of clouds and the jump distance.
The second line contains  space-separated integers  where . Each cloud is described as follows:

  • If , then cloud  is a cumulus cloud.
  • If , then cloud  is a thunderhead.


Output Format

Print the final value of  on a new line.

Sample Input

8 2
0 0 1 0 0 1 1 0

Sample Output



In the diagram below, red clouds are thunderheads and purple clouds are cumulus clouds:

game board

Observe that our thunderheads are the clouds numbered , , and . Aerith makes the following sequence of moves:

  1. Move: , Energy: .
  2. Move: , Energy: .
  3. Move: , Energy: .
  4. Move: , Energy: .

Solution in Python

def jumpingOnClouds(c, k):
    i , s = 0 , 100
    while (i+k)%n: 
        s -= 3 if c[(i+k)%n] else 1
        i += k
    s -= 3 if c[0] else 1
    return s
n , k = map(int,input().split())
c = list(map(int,input().split()))
print(jumpingOnClouds(c, k))

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.