Hello Programmers, In this post, you will know how to solve the HackerRank Jumping on the Clouds Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Jumping on the Clouds Solution
Task
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.
Constraints
- 2 <= n <= 100
- c[i] = {0, 1}
- c[0] = c[n – 1] = 0
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:
HackerRank Jumping on the Clouds Solution
Jumping on the Clouds Solution in C
#include<stdio.h> #include<math.h> int main(int argc, char const *argv[]) { int n,i,count=0; scanf("%d",&n); int arr[n]; for(i=0;i<n;i++) { scanf("%d",&arr[i]); } for(i=0;i<n-1;i++) { if(arr[i+2]==0) { count++; i=i+1; } else count++; } printf("%d\n",count); return 0; }
Jumping on the Clouds Solution in Cpp
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int main(){ int n; cin >> n; vector<int> c(n); for(int c_i = 0;c_i < n;c_i++){ cin >> c[c_i]; } vector<int>d(n, 10000); d[0] = 0; for (int i = 0; i < n; ++i) { if (c[i] == 1) continue; if (i + 1 < n && c[i + 1] == 0) { d[i + 1] = min(d[i + 1], d[i] + 1); } if (i + 2 < n && c[i + 2] == 0) { d[i + 2] = min(d[i + 2], d[i] + 1); } } cout << d[n - 1] << endl; return 0; }
Jumping on the Clouds 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 input = new Scanner(System.in); int rounds = input.nextInt(); int[] ar = new int[rounds]; int i = 0; for(i = 0; i < rounds; i++) ar[i] = input.nextInt(); int count = 0; i = 0; while(i != rounds-1) { if(i != ar.length - 2 && ar[i+2] == 0) i+=2; else i++; count++; } System.out.println(count); } }
Jumping on the Clouds Solution in Python
#!/bin/python import sys N = int(raw_input().strip()) A = map(int,raw_input().strip().split(' ')) #1 if bad memo = {} def dp(x): #min from x if x == N-1: return 0 if x >= N: return 10000000 if x in memo: return memo[x] ans = 1000000 if not A[x+1]: ans = 1+min(ans, dp(x+1)) if x+2 < N and not A[x+2]: ans = 1+min(ans,dp(x+2)) memo[x] = ans return ans print dp(0)
Jumping on the Clouds 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 = parseInt(readLine()); c = readLine().split(' '); c = c.map(Number); var counter = i = 0; while (i <= n) { if (!c[i + 2]) { i += 2; } else if (!c[i + 1]) { i += 1; } counter++; } console.log(counter - 1); }
Jumping on the Clouds Solution in Scala
import scala.io.StdIn object Solution { def main(args: Array[String]) { val n = StdIn.readInt() val th = StdIn.readLine().split("\\s+").map(_ == "1") val ans = Array.fill(n)(Int.MaxValue) ans(0) = 0 for (i <- 1 until n) { if (!th(i)) { ans(i) = ans(i - 1) + 1 if (i > 1) { ans(i) = math.min(ans(i - 1), ans(i - 2)) + 1 } } } println(ans(n - 1)) } }
Jumping on the Clouds Solution in Pascal
var a:array[0..1000] of longint; p,n,i,ans:longint; begin read(n); for i:=1 to n do read(a[i]); p:=1; while (p<n) do begin if (a[p+2]=0) then begin inc(p,2); inc(ans); end else begin inc(ans); inc(p); end; end; writeln(ans); end.
Disclaimer: This problem (Jumping on the Clouds) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.