HackerRank Jumping on the Clouds: Revisited Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Jumping on the Clouds: Revisited Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Jumping on the Clouds: Revisited Solution
HackerRank Jumping on the Clouds: Revisited Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Jumping on the Clouds: Revisited

Task

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

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

Given the values of nk, and the configuration of the clouds as an array c, determine the final value of e after the game ends.

Example. c = [0, 0, 1, 0]
k = 2

The indices of the path are 0 > 2 -> 0. The energy level reduces by 1 for each jump to 98. The character landed on one thunderhead at an additional cost of 2 energy units. The final energy level is 96.

Note: Recall that % refers to the modulo operation. In this case, it serves to make the route circular. If the character is at c[n – 1] and jumps 1, it will arrive at c[0]

Function Description

Complete the jumpingOnClouds function in the editor below.

jumpingOnClouds has the following parameter(s):

  • int c[n]: the cloud types along the path
  • int k: the length of one jump

Returns

  • int: the energy level remaining.

Input Format

The first line contains two space-separated integers, n and k, the number of clouds and the jump distance.
The second line contains n spaceseparated integers c[i] where 0 <= i < n. Each cloud is described as follows:

  • If c[i] = 0, then cloud i is a cumulus cloud.
  • If c[i] = 1, then cloud i is a thunderhead

Constraints

  • 2 <= n <= 25
  • 1 <= k <= n
  • n % k = 0
  • c[i] = {0, 1}

Sample Input

STDIN Function
—– ——–
8 2 n = 8, k = 2
0 0 1 0 0 1 1 0 c = [0, 0, 1, 0, 0, 1, 1, 0]

Sample Output

92

Explanation

In the diagram below, red clouds are thunderheads and purple clouds are cumulus clouds:

game board

Observe that our thunderheads are the clouds numbered 25, and 6. The character 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.

HackerRank Jumping on the Clouds: Revisited Solution

Jumping on the Clouds: Revisited Solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n,i,e=100; 
    int k; 
    scanf("%d %d",&n,&k);
    int *c = malloc(sizeof(int) * n);
    for(int c_i = 0; c_i < n; c_i++){
       scanf("%d",&c[c_i]);
    }
    do
        {
        i=(i+k)%n;
        if(c[i]==1)
            e=e-2;
        e--;
    }while(i!=0);
    printf("%d",e);
        
    return 0;
}

Jumping on the Clouds: Revisited Solution in Cpp

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

int main() {
	int n; int k;
	while(~scanf("%d%d", &n, &k)) {
		vector<int> c(n);
		for(int i = 0; i < n; ++ i)
			scanf("%d", &c[i]);
		int i = 0, E = 100;
		do {
			-- E;
			(i += k) %= n;
			if(c[i] == 1)
				E -= 2;
		} while(i != 0);
		printf("%d\n", E);
	}
	return 0;
}

Jumping on the Clouds: Revisited Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int c[] = new int[n];
        for(int c_i=0; c_i < n; c_i++){
            c[c_i] = in.nextInt();
        }
        
        int curr = 0;
        int e = 100;
        curr = (curr+k)%n;
        e -= 1+c[curr]*2;
        while (curr != 0) {
            curr = (curr+k)%n;
            e -= 1+c[curr]*2;
        }
        System.out.println(e);
    }
}

Jumping on the Clouds: Revisited Solution in Python

#!/bin/python

import sys


n,k = raw_input().strip().split(' ')
n,k = [int(n),int(k)]
c = map(int,raw_input().strip().split(' '))
cur = 0
e = 100
while True:
    if c[cur]==1:
        e-=2
    e-=1
    cur = (cur+k)%n
    if cur==0:
        break
print e

Jumping on the Clouds: Revisited Solution using JavaScript

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var n_temp = readLine().split(' ');
    var n = parseInt(n_temp[0]);
    var k = parseInt(n_temp[1]);
    c = readLine().split(' ');
    c = c.map(Number);
    
    var pos = 0;
    var E = 100;
    do {
        pos = (pos + k) % n;
        E -= 1;
        if (c[pos] === 1) {
            E -= 2;
        }
    } while (pos != 0);

    process.stdout.write("" + E);
}

Jumping on the Clouds: Revisited Solution in Scala

import scala.io.StdIn

object Solution {
  def main(args: Array[String]) {
    val Array(n, k) = StdIn.readLine().split("\\s+").map(_.toInt)
    val c = StdIn.readLine().split("\\s+").map(_.toInt)
    var e = 100
    var i = k % n
    e -= 1
    if (c(i) == 1) {
      e -= 2
    }
    while (i != 0) {
      i = (i + k) % n
      e -= 1
      if (c(i) == 1) {
        e -= 2
      }
    }
    println(e)
  }
}

Jumping on the Clouds: Revisited Solution in Pascal

var f:text; n,e,i,c,k:longint;
begin
  assign(f,''); reset(f); read(f,n,k); e:=100;
  for i:=0 to n-1 do
  begin
    read(f,c);
    if i mod k=0 then
    begin
      dec(e);
      if c=1 then dec(e,2)
    end
  end;
  close(f);
  assign(f,''); rewrite(f); write(f,e); close(f)
end.

Disclaimer: This problem (Jumping on the Clouds) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi


Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment