Jumping on the Clouds Revisited Hackerrank Solution in C++

 Jumping on the Clouds Revisited


Code

#include <bits/stdc++.h>

using namespace std;


int main()

{

    int n,k,i,e=100;

    cin>>n>>k;                             // taking input size of array and jump size

    int c[n];

    for(i=0;i<n;i++)

    {

        cin>>c[i];                 // input array

    }

    if(k>=n)             // if the jump size is greater than size of array then it will under go in a cyclic loop and start from first

    {

        i=k-n;                     //setting the cloud value after first jump 

        k--;

    }

    else

    {

        i=k;             // setting the cloud value after the first jump

    }

   

 for(i=i;i<n;i=i+k)                         // loop to find output

    {

        if(c[i]==1)        // after the jump if the cloud is thunder cloud then 3 energy level will reduce

        {

            e-=3;

        }

        else                 //since the cloud is not thunder cloud  only 1 energy level will reduce

        {

            e--;

        }

      

        if(i+k>=n)             // if the next jump exceeds array size then i should be reduce to perform cyclic rotation

        {

            i=i-n;

        } if(i==0)

        {

            break;

        }

    }

   

    cout<<e;                    // remaining energy level is printed

    

    return 0;

}




Jumping on the Clouds Revisited Hackerrank Solution in C++

Jumping on the Clouds Revisited Hackerrank Solution in Cpp

Post a Comment

0 Comments