HackerRank Jumping on the Clouds Solution
In this post, We will solve HackerRank Jumping on the Clouds Problem Solution.
There is a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. The player can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2. The player must avoid the thunderheads. Determine the minimum number of jumps it will take to jump from the starting postion to the last cloud. It is always possible to win the game.
For each game, you will get an array of clouds numbered 0 if they are safe or 1 if they must be avoided.
Example
c = [0, 1, 0, 0, 0, 1, 0]
Index the array from 0…6. The number on each cloud is its index in the list so the player must avoid the clouds at indices 1 and 5. They could follow these two paths: 0→2→4→ 6 or 0→2→3→4→ 6. The first path takes 3 jumps while the second
takes 4. Return 3.
Function Description
Complete the jumpingOnClouds function in the editor below.
jumpingOnClouds has the following parameter(s):
- int c[n]: an array of binary integers
Returns
- int: the minimum number of jumps required
Input Format
The first line contains an integer n, the total number of clouds. The second line contains n
space-separated binary integers describing clouds c[i] where 0 ≤ i ≤n.
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
The player must avoid c[2] and c[5]. The game can be won with a minimum of 4 jumps
Sample Input 1
6
0 0 0 0 1 0
Sample Output 1
3
Explanation 1
The only thundercloud to avoid is c[4]. The game can be won in 3 jumps
Jumping on the Clouds 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 jumps = 0;
scanf("%d",&n);
int *c = malloc(sizeof(int) * n);
for(int c_i = 0; c_i < n; c_i++){
scanf("%d",&c[c_i]);
}
for(int c_i = 0; c_i < n; c_i++){
if(c_i!=n-1){
if (c_i==n-2){
jumps++;
}
else if (c[c_i+1] == 1){
jumps++;
c_i++;
}
else if(c[c_i+2] == 1){
jumps++;
}
else if(c[c_i+2] == 0){
jumps++;
c_i++;
}
}
}
printf("%d",jumps);
return 0;
}
Jumping on the Clouds C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for(auto &it: a)
cin >> it;
int dp[n + 1];
const int inf = 1e9;
dp[0] = 0;
dp[1] = a[0] ? inf : 0;
for(int i = 2; i <= n; i++)
dp[i] = a[i - 1] ? inf : min(dp[i - 1], dp[i - 2]) + 1;
cout << dp[n] << "\n";
return 0;
}
Jumping on the Clouds C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
int n = Convert.ToInt32(Console.ReadLine());
string[] c_temp = Console.ReadLine().Split(' ');
int[] c = Array.ConvertAll(c_temp, Int32.Parse);
var index = 0;
var jump = 0;
while (index < n - 1)
{
var step = index + 2;
if (step < n && c[step] == 0)
{ index += 2; }
else
{ index++; }
jump++;
}
Console.WriteLine(jump);
}
}
Jumping on the Clouds Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'jumpingOnClouds' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY c as parameter.
*/
public static int jumpingOnClouds(List<Integer> c) {
// Write your code here
int count = 0;
int i=0;
while(i<c.size()-1)
{
if(i<c.size()-2 && c.get(i+2)==0)
{
i+=2;
count+=1;
}
else
{
i+=1;
count+=1;
}
}
return count;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> c = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = Result.jumpingOnClouds(c);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Jumping on the Clouds 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 main() {
var numClouds, clouds, numJumps, i;
numClouds = +readLine();
clouds = readLine().split(' ').map(Number);
numJumps = 0;
i = 0;
while (i < numClouds - 1) {
i += clouds[i + 2] === 0 ? 2 : 1;
numJumps++;
}
console.log(numJumps);
}
Jumping on the Clouds Python Solution
#!/bin/python3
import sys
n = int(input().strip())
c = [int(c_temp) for c_temp in input().strip().split(' ')]
jumps = 0
i = 0
len_c = len(c)
while i < (len_c - 1):
i_2 = i + 2
if i_2 < len_c and not c[i_2]:
i += 2
else:
i += 1
jumps += 1
print(jumps)
Other Solutions