Daily HackerRank Challenge (30 Part Series)

# About

This is a series of Daily HackerRank Challenge. Each day I show the some solutions written in C++.

# Jumping on the Clouds

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 jumps while the second takes 4.

Sample Input

```
7
0 0 1 0 0 1 0
```

Sample Output

```
4
```

If the next cloud `i+1`

is thunderhead, we need to jump from `i`

to `i+2`

. If not, jump to `i+1`

only.

Final Solution

```
int n,c[100],j=0;
int main()
{
FAST_INP;
cin >> n;
REP(i,n) cin >> c[i];
REP(i,n-1){
if(i<n-2&&c[i+2]==0) i++;
j++;
}
cout << j;
return 0;
}
```

# Jumping on the Clouds: Revisited

Aerith is playing a cloud hopping game. In this game, there are sequentially numbered clouds that can be thunderheads or cumulus clouds. Her character must jump from cloud to cloud until it reaches the start again.

To play, Aerith is given an array of clouds, c and an energy level e=100. She starts from c[0] and uses 1 unit of energy to make a jump of size k to cloud c[(i+k)%n]. If Aerith lands on a thundercloud,c[i]=1, her energy (e) decreases by 2 additional units. The game ends when Aerith lands back on cloud 0.

Given the values of n,k, and the configuration of the clouds as an array c, can you determine the final value of e after the game ends?

For example, give c=[0,0,1,0] and k=2, the indices of her path are 0->2>0. Her energy level reduces by 1 for each jump to 98. She landed on one thunderhead at an additional cost of energy units. Her final energy level is 96.

Sample Input

```
8 2
0 0 1 0 0 1 1 0
```

Sample Output

```
92
```

For every 2 clouds, we deduct the energy by 1. If it is a thunderhead, deduct it by 2.

Final Solution

```
int n,k,c[25],e=100;
int main()
{
FAST_INP;
cin >> n >> k;
REP(i,n){
cin >> c[i];
if(i%k==0){
if(c[i]==1) e-=2;
e--;
}
}
cout << e;
return 0;
}
```

## Note:

The sample test case 1 and test case 8 violates the constraint `n%k=0`

. You never get it correct.

```
10 3
1 1 1 0 1 1 0 0 0 0
```

# Complete Code

Check out the complete code via below links

Daily HackerRank Challenge (30 Part Series)

Posted on Apr 27 by:

### Wing-Kam

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.

## Discussion