Jumping on the Clouds: Revisited

Jumping on the Clouds: Revisited

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

To play, Aerith is given an array of clouds, c and an energy level e = 100. She starts from c[0] and uses 1 unit of energy to make a jump of size k to cloud c[(i + k) % n]. If Aerith lands on a thundercloud, c[i] = 1, 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 as an array c, can you determine the final value of e after the game ends?

For example, give c = [0, 0, 1, 0] and k = 2, the indices of her path are 0 -> 2 -> 0. Her energy level reduces by 1 for each jump to 98. She landed on one thunderhead at an additional cost of 2 energy units. Her final energy level is 96.

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

Function Description

Complete the jumpingOnClouds function in the editor below. It should return an integer representing the energy level remaining after the game.

jumpingOnClouds has the following parameter(s):

  • c: an array of integers representing cloud types
  • k: an integer representing the length of one jump

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 . 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

  • n % k = 0

Output Format

Print the final value of e on a new line.

Sample Input

1
2
8 2
0 0 1 0 0 1 1 0

Sample Output

1
92

Explanation

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

Observe that our thunderheads 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.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Complete the jumpingOnClouds function below.
function jumpingOnClouds(c, k) {
let result = 100;
let count = 0;
let index;

while (index !== 0 && result >= 0) {
!count && (index = 0);

index = (index + k) % c.length;

result = result - (c[index] > 0 ? 3 : 1);

count++;
}

return result;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×