In this HackerRank Jumping on the clouds interview preparation kit problem For each game, you will get an array of clouds numbered 0 if they are safe or 1 if they must be avoided.
Problem solution in Python programming.
#!/bin/python3 import math import os import random import re import sys # Complete the jumpingOnClouds function below. def jumpingOnClouds(c): x,y = 0,0 while x<len(c)-2: x = x+1 if c[x+2] else x+2 y+=1 if x<len(c)-1: y+=1 return y if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') n = int(input()) c = list(map(int, input().rstrip().split())) result = jumpingOnClouds(c) fptr.write(str(result) + 'n') fptr.close()
Problem solution in Java Programming.
import java.io.*; import java.util.*; public class Solution { private static InputReader in; private static PrintWriter out; public static void main(String[] args) throws IOException { in = new InputReader(System.in); out = new PrintWriter(System.out, true); int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } int[] dp = new int[n]; for (int i = 1; i < n; i++) { if (arr[i] == 1) dp[i] = 1 << 29; dp[i] = 1 << 29; if (i >= 1 && arr[i-1] == 0) dp[i] = Math.min(dp[i], dp[i-1]+1); if (i >= 2 && arr[i-2] == 0) dp[i] = Math.min(dp[i], dp[i-2]+1); } out.println(dp[n-1]); out.close(); System.exit(0); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Problem solution in C++ programming.
#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, INT_MAX/2); for(int i = 0;i < n;i++){ int x; cin >> x; if (x == 1) continue; if (i == 0) c[i] = 0; else if (i == 1) c[i] = 1 + c[0]; else c[i] = min(c[i-1], c[i-2]) + 1; } cout << c.back() << endl; return 0; }
Problem solution in C programming.
#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; scanf("%d",&n); int *c = malloc(sizeof(int) * n); for(int c_i = 0; c_i < n; c_i++){ scanf("%d",&c[c_i]); } int i, count=0; for(i=0;i<n;i++){ if(i+1==n-1 || i+2==n-1){ count++; printf("%dn",count); break; } else if(c[i+2]==0){ i++; count++; } else if(c[i+1]==0){ count++; } } return 0; }
Problem solution in JavaScript programming.
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 i = 0; var jump = 0; while (i < n) { if (c[i+2] === 0) { jump++; i += 2; } else if (c[i+1] === 0) { jump++; i++; } else { // Thunder Cloud... i++; } } console.log(jump); }