Coding with Kev: Jumping on the Clouds (HackRank) Walk-Through

Photo by Pero Kalimero on Unsplash

Welcome back to my blog! I thought I would start a new segment called Coding with Kev where I’ll work through a code challenge with you and walk you through my thought process. I feel that it would be helpful for those starting out and practicing their programming skills. Please understand that I am no expert and there are always a multitude of possible answers for each problem. Well let’s get started!

Introduction

This week we will be working on a HackerRank coding challenge called Jumping on the Clouds. Please use either of the links to see the actual problem on HackerRank and work through it with me.

I will do a quick run through of the instructions but for the full instructions please read through the problem on HackerRank. But, essentially, you are given an array of 0’s and 1’s, which represent clouds you are able to jump on and ones that you cannot. You are able to jump on 0’s, but cannot jump on 1’s. The goal is to find the minimum number of jumps it would take to get to the last cloud in the array. I know that sounds fairly simple, but there is one final piece that makes it a little more difficult. The player can jump on any cloud (0) having an index that is equal to the index of the current cloud plus 1 or 2. Please look at the example below to get a better understanding of the input (array of 0’s and 1’s) and the expected output (minimum number of jumps).

Example from HackerRank

In the sample above, c is the array that is given and 3 is the expected output for the function you are asked to build out. Now that we have all of the basics of the problem, let’s dive right in!

The Problem

When I first saw this problem, the first thing I thought about was finding the indexes for each element in the array, c, that were equal to 0. So the way I tried to approach getting the indexes was by iterating through array, c, and pushing the index of each element equal to 0 into a new array named safeClouds.

Finding indexes of safeClouds

Now, I have all of indexes in the safeCloud array but still need to find out the minimum number of jumps required to get to the last element. So, my plan at this point was to iterate through the array of indexes and count the number of jumps (minJumps) based on whether the following index was equal to +1 or +2 of the current index. If that constraint wasn’t met, no jump would be added to minJumps. Let’s look at the code below.

Iterating through safeClouds array and counting number of jumps

For the iteration above, the conditions I used were to check to see if there were consecutive clouds we could jump over (consecutive 0’s) as 1 jump or if there was only one jump (one 0 then 1) to the immediate next cloud. Finally, all I need to do is return the minJump variable to get the minimum number of jumps required to get to the end. Below is my full function.

Completed jumpingOnClouds function

Conclusion

Well that was a quick run walk through of the Jumping on Clouds problem on HackerRank. As I said in the beginning, there are tons of different solutions to this problem and honestly, I guarantee there are more efficient solutions than mine. But, this is where my mind went and how I solved out this challenge, which cleared all of the tests. I really hope this will help spark some inspiration or creative juices on the next coding problem you encounter. I also challenge you to try to find a different solution to this problem and also take the time to look at the other solutions from other developers. You would be surprised how much you can learn from other people’s code! Thanks again for joining and I hope to see you next week!