-2

It is a problem from hackerrank.

https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem?h_l=interview&isFullScreen=false&playlist_slugs%5B%5D%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D%5B%5D=warmup

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int jump=0;
    int a[101];
    a[0]=0;
    for (int i = 1; i < n; i++) {
        cin>> a[i];
    }
    for(int j=1;j<n;j++)
    {
        if(a[j]==1)
        {
            jump++;
        }
        if(a[j]==0)
        {
            jump++;
            if(a[j-1]==0 && a[j-1]==1)
                jump--;
        }
    }
    cout<<jump;
    return 0;
}

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 postion 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:0->2->4->6 or 0->2->3->4->6 . The first path takes 3 jumps while the second takes 4. So, we have to print the minimum number of jumps needed to win the game.

Here's the input/output:

7
0 0 1 0 0 1 0

Expected output is: 4

My wrong output : 6

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    **Protip:** [Why should I not #include ?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) – Rohan Bari Aug 11 '20 at 09:51
  • Also, what's your input/output to produce the error? – Rohan Bari Aug 11 '20 at 09:53
  • @RohanBari n=7 , a[]=0 0 1 0 0 1 0 Expected output -> 4 but it says no response on stdout – Sai Ganapriya Aug 11 '20 at 09:55
  • There is something wrong with the input versus the code. You say you give 7 followed by 7 0/1. However your code only reads 6 0/1 Do you get the input data shifted? – Support Ukraine Aug 11 '20 at 10:01
  • 1
    You are not incrementing your iteration variable 'j' – Jasmeet Aug 11 '20 at 10:02
  • I don't see any code that tries to jump 2 steps... – Support Ukraine Aug 11 '20 at 10:03
  • @4386427 Sorry, I am afraid that I'm not aware of shifting any input data here . I'm new to this :( I gave my array size as a[101] ; – Sai Ganapriya Aug 11 '20 at 10:06
  • @Jasmeet Ohh!!! Right . Thanks ! I didn't see that .I'll try – Sai Ganapriya Aug 11 '20 at 10:07
  • You need to learn debugging. Print all variables each iteration to see what happens. – klutt Aug 11 '20 at 10:09
  • And don't tag spam. This was not C. – klutt Aug 11 '20 at 10:09
  • @SaiGanapriya I think you are approaching this in an incorrect way. When you are going to jump, the best you can do is to jump 2 positions forward. If you can't do that you'll have to jump a single position forward. Something like: `if (a[current_pos + 2] == 0) {current_pos += 2;} else {current_pos += 1;} ++jump;` and wrap that in a while-loop like: `while (current_pos < (n-1)) { ... }` – Support Ukraine Aug 11 '20 at 10:11
  • @4386427 Thanks a lot ! It worked... I thought to jump single step by step and then to jump back if needed. But your way is more efficient and correct. – Sai Ganapriya Aug 11 '20 at 10:42

2 Answers2

3

The first obvious problem I see is that the second loop will never end, because j is never incremented. Furthermore the condition

if(a[j-1]==0 && a[j-1]==1)

makes no sense. a[j-1] can only be either 0 or 1, not both unless you are running this on a quantum computer. Fix these issues and work from there.

A.Hristov
  • 481
  • 1
  • 4
  • 13
0

Loop through every position in the consecutive cloud List advancing your position 2 jumps forward if possible. Otherwise, you just jump once. Regardless, you'll count the number of iterations taken until you've reached the end. Also, because each iteration is a jump, you don't need to jump off the last cloud, so go to size() minus one.

Time-complexity: O(n) [n = number of clouds]

Space-complexity: O(1)

Java 8

public static int jumpingOnClouds(List<Integer> c) 
{
    //Track number of jumps
    int jumpCount = 0;
    
    //Keep jumping until you can't anymore
    for(int i = 0; i < c.size()-1; jumpCount++)
    {
        //If possible, long jump (2 clouds forward)
        if(i < c.size() - 2 && c.get(i+2) != 1)
            i+=2;
        else //Otherwise, short jump (1 cloud forward)
            i++;
    }
    
    //Return count of jumps taken
    return jumpCount;
}