Recent Tutorials and Articles
    HackerRank Sol: Jumping on the Clouds Revisited
    Published on: 18th July 2016

    This tutorial provides Java solution to "Jumping on the clouds revisited" challenge of HackerRank.

    Hackerrank Challenge Details


    Problem Statement:

    Aerith is playing a cloud game! In this game, there are n clouds numbered sequentially from 0 to n - 1. Each cloud is either an ordinary cloud or a thundercloud.

    Aerith starts out on cloud  with energy level E = 100. She can use 1 unit of energy to make a jump of size k to cloud (i + k) % n until she gets back to cloud 0. If Aerith lands on a thundercloud, 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, can you determine the final value of E after the game ends?

    Note: Recall that  refers to the modulo operation.

    Input Format:

    The first line contains two space-separated integers, n (the number of clouds) and k (the jump distance), respectively. 
    The second line contains n space-separated integers describing the respective values of clouds c0, c1,..., cn-1. Each cloud is described as follows:

    • If ci = 0, then cloud i is an ordinary cloud.
    • If ci = 1, then cloud i is a thundercloud.

    Output Format:

    Print the final value of E on a new line.

    Constraints:

    • 0 <= A,B <= 2 
    • 3 <= N <= 20

    Sample Input:

    8 2
    0 0 1 0 0 1 1 0

    Sample Output:

    92

    Explanation:

    In the diagram below, red clouds are thunderclouds and purple clouds are ordinary clouds:

    game board

    Observe that our thunderclouds are the clouds numbered 2, 5, and 6. Aerith makes the following sequence of moves:

    1. Move: 0 -> 2, Energy: E = 100 - 1 - 2 = 97.
    2. Move: 2 -> 4, Energy: E = 97 - 1 = 96.
    3. Move: 4 -> 6, Energy: E = 96 - 1 - 2 = 93.
    4. Move: 6 -> 0, Energy: E = 93 - 1 = 92.

    Thus, we print 92 as our answer.

    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-revisited
     *
     */
    public class JumpingOnCloudsRevisited {
    	
    	public static void main(String[] args) {
    		final Scanner in = new Scanner(System.in);
    		
    		final int n = in.nextInt();
    		final int k = in.nextInt();
    		int E = 100;
    		
    		for(int i = 0; i < n; i++) {
    			final int num = in.nextInt();
    			// if cloud num is divisible by k , subtract 1 as well as 2 if num is 1.
    			if( i % k == 0) {
    				E -= (1 + num * 2);
    			}
    		}
    		
    		System.out.println(E);
    		
    		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.

    Published on: 18th July 2016