In this post, we will solve Jumping on the Clouds: Revisited HackerRank Solution. This problem (Jumping on the Clouds: Revisited) is a part of HackerRank Algorithms series.
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 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]
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 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.
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
Observe that our thunderheads are the clouds numbered 2, 5, and 6. The character makes the following sequence of moves:
- Move: 0 -> 2, Energy: e = 100 – 1 – 2 = 97.
- Move: 2 -> 4, Energy: e = 97 – 1 = 96.
- Move: 4 -> 6, Energy: e = 96 – 1 – 2 = 93.
- Move: 6 -> 0, Energy: e = 93 – 1 = 92.
Solution – Jumping on the Clouds: Revisited – HackerRank Solution
C++
#include <bits/stdc++.h> #define FO(i,a,b) for (int i = (a); i < (b); i++) #define sz(v) int(v.size()) using namespace std; int n, k, e; int t[105]; int main() { scanf("%d %d", &n, &k); e = 100; FO(i,0,n) scanf("%d", t+i); int i = 0; while (1) { e -= 2*t[i]+1; i = (i+k)%n; if (i == 0) break; } printf("%d\n", e); }
Python
#!/bin/python3 import sys def jumpingOnClouds(c, k): cur = k % n energy = 100 - 1 - c[cur]*2 while cur != 0: cur = (cur + k) % n energy -= 1 + c[cur]*2 return energy if __name__ == "__main__": n, k = input().strip().split(' ') n, k = [int(n), int(k)] c = list(map(int, input().strip().split(' '))) result = jumpingOnClouds(c, k) print(result)
Java
import java.util.*; public class A { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); int[] array = new int[n]; for(int x = 0; x < n; x++) { array[x] = in.nextInt(); } int cloud = 0; int e = 100; for(int y = 0; y < n; y++) { cloud = (cloud + k) % n; e--; if(array[cloud] == 1) { e -= 2; } if(cloud == 0) { break; } } System.out.println(e); } }
Note: This problem (Jumping on the Clouds: Revisited) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.
Also Read: