Jumping on the Clouds


Problem Statement :


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.



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


Output Format

Print the minimum number of jumps needed to win the game.



Solution :



title-img


                            Solution in C :

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;
}
                        


                        Solution in C++ :

In   C++  :






#include<bits/stdc++.h>

using namespace std;

int a[100], dp[100];

int main() {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) scanf("%d", a + i);
	
	dp[0] = 0;
	for (int i = 1; i < n; i++) {
		dp[i] = dp[i - 1] + 1;
		if (i > 1) dp[i] = min(dp[i], dp[i - 2] + 1);
		
		if (a[i] == 1) dp[i] = n + 1;
	}
	
	printf("%d\n", dp[n - 1]);

	return 0;
}
                    


                        Solution in Java :

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);
    }
}
                    


                        Solution in Python : 
                            
In  Python3   :







#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the jumpingOnClouds function below.
def jumpingOnClouds(c):
    i = 0
    jumpCount = -1
    while i < len(c):
        jumpCount += 1
        if (i < len(c)-2) and (c[i+2] == 0):
            i+=1
        i += 1
    return jumpCount
            
            
        

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()
                    


View More Similar Problems

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →

Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →