# HackerRank Jumping on the Clouds: Revisited Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Jumping on the Clouds: Revisited Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

Table of Contents

## HackerRank Jumping on the Clouds: Revisited

### Task

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 nk, 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 spaceseparated 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

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

Observe that our thunderheads are the clouds numbered 25, 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.

## HackerRank Jumping on the Clouds: Revisited Solution

### HackerRank Jumping on the Clouds: Revisited Solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
int n,i,e=100;
int k;
scanf("%d %d",&n,&k);
int *c = malloc(sizeof(int) * n);
for(int c_i = 0; c_i < n; c_i++){
scanf("%d",&c[c_i]);
}
do
{
i=(i+k)%n;
if(c[i]==1)
e=e-2;
e--;
}while(i!=0);
printf("%d",e);

return 0;
}

### HackerRank Jumping on the Clouds: Revisited Solution in Cpp

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

int main() {
int n; int k;
while(~scanf("%d%d", &n, &k)) {
vector<int> c(n);
for(int i = 0; i < n; ++ i)
scanf("%d", &c[i]);
int i = 0, E = 100;
do {
-- E;
(i += k) %= n;
if(c[i] == 1)
E -= 2;
} while(i != 0);
printf("%d\n", E);
}
return 0;
}

### HackerRank Jumping on the Clouds: Revisited Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int c[] = new int[n];
for(int c_i=0; c_i < n; c_i++){
c[c_i] = in.nextInt();
}

int curr = 0;
int e = 100;
curr = (curr+k)%n;
e -= 1+c[curr]*2;
while (curr != 0) {
curr = (curr+k)%n;
e -= 1+c[curr]*2;
}
System.out.println(e);
}
}

### HackerRank Jumping on the Clouds: Revisited Solution in Python

#!/bin/python

import sys

n,k = raw_input().strip().split(' ')
n,k = [int(n),int(k)]
c = map(int,raw_input().strip().split(' '))
cur = 0
e = 100
while True:
if c[cur]==1:
e-=2
e-=1
cur = (cur+k)%n
if cur==0:
break
print e

### HackerRank Jumping on the Clouds: Revisited Solution using JavaScript

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
input_stdin += data;
});

process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});

function readLine() {
return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
var n_temp = readLine().split(' ');
var n = parseInt(n_temp[0]);
var k = parseInt(n_temp[1]);
c = readLine().split(' ');
c = c.map(Number);

var pos = 0;
var E = 100;
do {
pos = (pos + k) % n;
E -= 1;
if (c[pos] === 1) {
E -= 2;
}
} while (pos != 0);

process.stdout.write("" + E);
}

### HackerRank Jumping on the Clouds: Revisited Solution in Scala

import scala.io.StdIn

object Solution {
def main(args: Array[String]) {
val Array(n, k) = StdIn.readLine().split("\\s+").map(_.toInt)
val c = StdIn.readLine().split("\\s+").map(_.toInt)
var e = 100
var i = k % n
e -= 1
if (c(i) == 1) {
e -= 2
}
while (i != 0) {
i = (i + k) % n
e -= 1
if (c(i) == 1) {
e -= 2
}
}
println(e)
}
}

### HackerRank Jumping on the Clouds: Revisited Solution in Pascal

var f:text; n,e,i,c,k:longint;
begin
assign(f,''); reset(f); read(f,n,k); e:=100;
for i:=0 to n-1 do
begin
read(f,c);
if i mod k=0 then
begin
dec(e);
if c=1 then dec(e,2)
end
end;
close(f);
assign(f,''); rewrite(f); write(f,e); close(f)
end.

Disclaimer: This problem (Jumping on the Clouds) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

Sharing Is Caring