This tutorial provides Java solution to "Jumping on the Clouds" challenge of HackerRank.
Hackerrank Challenge Details
Problem Statement:
Emma is playing a new mobile game involving n clouds numbered from 0 to n - 1. A player initially starts out on cloud c0, and they must jump to cloud cn-1. In each step, she can jump from any cloud to cloud or cloud .
There are two types of clouds, ordinary clouds and thunderclouds. The game ends if Emma jumps onto a thundercloud, but if she reaches the last cloud (i.e., cn-1), she wins the game!
Can you find the minimum number of jumps Emma must make to win the game? It is guaranteed that clouds c0 and cn-1 are ordinary-clouds and it is always possible to win the game.
Input Format:
The first line contains an integer, n (the total number of clouds).
The second line contains n space-separated binary integers describing clouds c0, c1, c2,..., cn-1.
- If ci = 0, the ith cloud is an ordinary cloud.
- If ci = 1, the ith cloud is a thundercloud.
Output Format:
Print the minimum number of jumps needed to win the game.
Constraints:
- 2 < n < 100
- ci € {0, 1}
- c0 = cn-1 = 0
Sample Input 0:
7
0 0 1 0 0 1 0
Sample Output 0:
4
Sample Input 1:
6
0 0 0 0 1 0
Sample Output 1:
3
Explanation:
Sample Case 0:
Because c2 and c5 in our input are both 1, Emma must avoid c2 and c5. Bearing this in mind, she can win the game with a minimum of 4 jumps:
Sample Case 1:
The only thundercloud to avoid is c4. Emma can win the game in 3 jumps:
Solution Details
Java Implementation:
package com.saintech.allprogtutorials.hackerrank.algos;
import java.util.Scanner;
/**
* @author Sain Technology Solutions
*
* Solution to Problem - https://www.hackerrank.com/challenges/jumping-on-the-clouds
*
*/
public class JumpingOnClouds {
public static void main(String[] args) {
final Scanner in = new Scanner(System.in);
final int N = in.nextInt();
// Put all the clouds in an array
final int[] clouds = new int[N];
for(int i = 0; i < N; i++) {
clouds[i] = in.nextInt();
}
// Check if two steps can be taken by verifying cloud type at
// second step. Otherwise check if one step can be taken by verifying
// if we still have any more clouds. If none of these conditions are met,
// break out of infinite loop.
int noOfJumps = 0, i = 0;
while(true) {
if(i + 2 < N && clouds[i + 2] == 0) {
i += 2;
} else if(i + 1 < N) {
i++;
} else {
break;
}
noOfJumps++;
}
System.out.println(noOfJumps);
in.close();
}
}
Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.