Jumping on the Clouds: Revisited HackerRank Solution in C, C++, Java, Python

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 n,k , 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]


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  and jumps , it will arrive at .

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


  • 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 space-separated 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.

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



Jumping on the Clouds: Revisited HackerRank 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++){
    return 0;


Jumping on the Clouds: Revisited HackerRank Solution in C++

#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 HackerRank 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;


Jumping on the Clouds: Revisited HackerRank Solution in 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:
    cur = (cur+k)%n
    if cur==0:
print e


Jumping on the Clouds: Revisited HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static void Main(String[] args) {
        var tmp = Console.ReadLine().Split(' ');
        int n = int.Parse(tmp[0]);
        int k = int.Parse(tmp[1]);
        int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), x => Convert.ToInt32(x));

        int E = 100;

        int pos = 0;
        while (true) {
            pos += k;
            pos %= n;
            if (arr[pos] == 1) E -= 2;
            E -= 1;
            if (pos == 0) break;


Attempt Jumping on the Clouds: Revisited HackerRank Challenge 

Link – https://www.hackerrank.com/challenges/jumping-on-the-clouds-revisited/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/find-digits-hackerrank-solution/



Leave a Comment