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 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 5555555555555555555555555555555555555555555555
Given an array of integers, determine the minimum number of elements to delete to leave only elements of equal value.
Example
Arr = [1,2,2,3]
Delete the 2 elements 1 and 3 leaving arr=[2,2]. If both twos plus either the 1 or 3 are deleted, it takes 3 deletions to leave either [3] or [1]. The minimum number of deletions is 2.
Function Description
Complete the equalizeArray function in the editor below.
equalizeArray has the following parameter(s):
- int arr[n]: an array of integers
Returns
- int: the minimum number of deletions required
Input Format
The first line contains an integer , the number of elements in arr.
The next line contains space-separated integers arr[i].
Constraints
- 1<=n<=100
- 1<=arr[i]<=100
Sample Input
STDIN Function ----- -------- 5 arr[] size n = 5 3 3 2 1 3 arr = [3, 3, 2, 1, 3]
Sample Output
2
Jumping on the Clouds HackerRank 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 HackerRank Solution in C++
#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 HackerRank 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 HackerRank 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 HackerRank Solution in C#
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); int count = 0; int index = 0; while (index < n - 1) { if (index + 2 <= n - 1 && c[index + 2] != 1) { index = index + 2; } else { index = index + 1; } count++; } Console.WriteLine(count); } }
Attempt Jumping on the Clouds HackerRank Challenge
Link – https://www.hackerrank.com/challenges/jumping-on-the-clouds/
Next HackerRank Challenge Solution
Link – https://exploringbits.com/queens-attack-2-hackerrank-solution/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.