Skip to main content

Jumping on the Clouds Hackerrank

Jumping on the Clouds solutions in c.


Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2 . She must avoid the thunderheads. Determine the minimum number of jumps it will take Emma to jump from her starting position to the last cloud. It is always possible to win the game.
For each game, Emma will get an array of clouds numbered 0  if they are safe or 1 if they must be avoided. For example, c=[0,1,0,0,0,1,0] indexed from 0....6. The number on each cloud is its index in the list so she must avoid the clouds at indexes 1 and 5. She could follow the following two paths:024➜6  or 02346. The first path takes 3  jumps while the second takes 4.
Function Description
Complete the jumpingOnClouds function in the editor below. It should return the minimum number of jumps required, as an integer.
jumpingOnClouds has the following parameter(s):
  • c: an array of binary integers
Input Format
The first line contains an integer , the total number of clouds. The second line contains  space-separated binary integers describing clouds  where .
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:
Emma must avoid  and . She can win the game with a minimum of  jumps:
Sample Input 1
6
0 0 0 0 1 0
Sample Output 1
3
Explanation 1:
The only thundercloud to avoid is . Emma can win the game in  jumps:


Solution to this problem is given below:

we can solve this problem in two steps.
  • First step will be to take input .

scanf("%d",&n);
    int s[n];
    for(i=0;i<n;i++){
        scanf("%d",&s[i]);
    }
  • Second step will be to count minimum number of jumps required.
   To understand let's take an example
6
0 0 0 0 1 0
 suppose this is the input now we know that she can jump only one 0 and 1 should be avoided.
she has two choice either she can jump continuously on 0 and avoid 1 or she can skip one jump and avoid 1. Means she can have max 5 jump or min 3 jump. For this problem we have to find min jump so she has to choose to second option.
Now for min jump to find we must check one condition and that condition is if ith position is 0 and i+2 is also 0 she can skip one jump. and if condition is false she that means there must be 1 at that position. 
we will count the jump and will print count.
for(i=0;i<n-1;i++){
        if(s[i]==0&&s[i+2]==0){
            count++;
            i++;
        }
        else {
        count++;
        }
    }   
I hope you all would have understand. If you get any confusion please comment down below. 

This is the full code 
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    int i,n,count=0;
    scanf("%d",&n);
    int s[n];
    for(i=0;i<n;i++){
        scanf("%d",&s[i]);
    }
    for(i=0;i<n-1;i++){
        if(s[i]==0&&s[i+2]==0){
            count++;
            i++;
        }
        else {
        count++;
        }
    }   
   printf("%d",count);
   return 0;
}




Comments

Popular posts from this blog

Day 2: Conditional Statements: Switch

  Objective In this challenge, we learn about  switch statements . Check out the attached tutorial for more details. Task Complete the  getLetter(s)  function in the editor. It has one parameter: a string,  , consisting of lowercase English alphabetic letters (i.e.,  a  through  z ). It must return  A ,  B ,  C , or  D  depending on the following criteria: If the first character in string   is in the set  , then return  A . If the first character in string   is in the set  , then return  B . If the first character in string   is in the set  , then return  C . If the first character in string   is in the set  , then return  D . Hint:  You can get the letter at some index   in   using the syntax  s[i]  or  s.charAt(i) . Input Format Stub code in the editor reads a single string denoting   from ...

Day 2: Conditional Statements: If-Else

Day 2: Conditional Statements: If-Else || Hackerrank Solution Objective In this challenge, we learn about  if-else  statements. Check out the attached tutorial for more details. Task Complete the  getGrade(score)  function in the editor. It has one parameter: an integer,  , denoting the number of points Julia earned on an exam. It must return the letter corresponding to her   according to the following rules: If  , then  . If  , then  . If  , then  . If  , then  . If  , then  . If  , then  .