HackerRank Jumping on the Clouds: Revisited
In this Post, We will solve HackerRank Jumping on the Clouds: Revisited.
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
In the diagram below, red clouds are thunderheads and purple clouds are cumulus clouds:
Observe that our thunderheads are the clouds numbered 2, 5, and 6. The character makes the following sequence of moves:
- Move: 02, Energy: e = 100-1-2=97.
- Move: 24, Energy: e = 97-1 = 96.
- Move: 46, Energy: e = 96-1-2 = 93.
- Move: 60, Energy: e = 93-1 = 92.
Jumping on the Clouds: Revisited C Solution
#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;
int k;
int i, j, eUsed = 0;
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]);
}
if (n<2 || n>25) return 100;
i = 0;
while (1) {
for (j=0; j<k; j++) {
i = (i+1)%n;
}
eUsed += 1;
if (c[i] == 1) eUsed += 2;
if (i == 0) break;
}
printf("%d\n", 100-eUsed);
return 0;
}
Jumping on the Clouds: Revisited C++ Solution
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll n,k,i,x,p,cnt=0;
cin>>n>>k;
for(i=0;i<n;i++)
{
cin>>p;
if(i%k==0)
{
if(p==1)
cnt+=2;
}
//cout<<cnt<<endl;
}
cnt+=(n/k);
//cout<<cnt<<endl;
cout<<100-cnt<<endl;
return 0;
}
Jumping on the Clouds: Revisited C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
string[] tokens_n = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(tokens_n[0]);
int k = Convert.ToInt32(tokens_n[1]);
string[] c_temp = Console.ReadLine().Split(' ');
int[] c = Array.ConvertAll(c_temp,Int32.Parse);
int E = 100;
for (int i = 0; i < n; i+=k)
{
E--;
if (c[i] == 1)
{
E -= 2;
}
}
Console.WriteLine(E);
}
}
Jumping on the Clouds: Revisited Java Solution
import java.io.*;
import java.util.*;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
class Result{
public static int jumpingOnClouds(int k,List<Integer> c){
int energy = 99;
int i = k;
while (i != 0){
if (i >= c.size()) {
i -= c.size();
if (i==0) break;
}
energy--;
if (c.get(i) == 1) energy -= 2;
i += k;
}
if (c.get(i) == 1) energy -= 2;
return energy;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$","").split(" ");
Integer n = Integer.parseInt(firstMultipleInput[0]);
Integer k = Integer.parseInt(firstMultipleInput[1]);
List<Integer> c= Stream.of(bufferedReader.readLine().replaceAll("\\s+$","").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = Result.jumpingOnClouds(k,c);
bufferedWriter.write(String.valueOf(result));
bufferedReader.close();
bufferedWriter.close();
}
}
Jumping on the Clouds: Revisited JavaScript Solution
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 getLeftEnergy(arr, jumpSize){
var energy = 100;
arr.push(arr[0])
for(var i = jumpSize; i < arr.length; i += jumpSize ){
if(arr[i] === 1){
energy -= 2;
}
--energy;
}
return energy;
}
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);
console.log(getLeftEnergy(c, k));
}
Jumping on the Clouds: Revisited Python Solution
#!/bin/python3
import sys
n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
c = [int(c_temp) for c_temp in input().strip().split(' ')]
e = 100
i = n # just so we don't leave the loop immediately
while i != 0:
i = (i + k) % n
e -= 1 + c[i%n]*2 # 1 per jump
print(e)
other solutions