Jumping on the Clouds: Revisited
Problem Statement :
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: 1. If c[i] = 0, then cloud i is a cumulus cloud. 2. If c[i] =1, then cloud i is a thunderhead. Constraints 2 <= n <= 25 1 <= k <= n n%k = 0 c[i] belongs to {0, 1}
Solution :
Solution in C :
python 3 :
#!/bin/python3
import sys
n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
c = [int(c_temp) for c_temp in input().strip().split(' ')]
e = 100
cur = 0
while e == 100 or cur:
cur = (cur + k) % n
e -= 1 + 2 * c[cur]
print(e)
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);
}
}
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);
}
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;
}
View More Similar Problems
Kundu and Tree
Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that
View Solution →Super Maximum Cost Queries
Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and
View Solution →Contacts
We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co
View Solution →No Prefix Set
There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio
View Solution →Cube Summation
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor
View Solution →Direct Connections
Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do
View Solution →