Header Ad

HackerRank Jumping on the Clouds: Revisited problem solution

In this HackerRank Jumping on the clouds: The revisited problem you have 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.

HackerRank Jumping on the Clouds: Revisited problem solution


Problem solution in Python programming.

n,k = map(int,input().strip().split())
c = [int(s) for s in input().strip().split()]
print(100-n//k-2*sum([c[i] for i in range(0,n,k)]))


Problem solution in Java Programming.

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 Energy = 100;
        while(curr < n ){
            Energy--;
            if(c[curr]==1)
                Energy-=2;
            curr += k;
        }
        System.out.println(Energy);
    }
    
}


Problem solution in C++ programming.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int n;
    int k;
    cin >> n >> k;
    vector<int> c(n);
    for(int c_i = 0;c_i < n;c_i++){
       cin >> c[c_i];
    }
    
    int e = 100;
    int cur = 0;
    while (true) {
        cur += k;
        cur %= n;
        if (c[cur]) e-= 2;
        e--;
        if (cur == 0) break;
    }
    cout << e << endl;
    return 0;
}


Problem solution in C programming.

#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; 
    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]);
    }
    
    int energy = 100;
    int i = 0;
    
    //make a first move
    i = (i + k) % n;
    if(c[i] == 0)
    {
        energy--;    
    } else if(c[i] == 1) {
        energy = energy - 3;
    }
    
    while(1)
    {
        if(i == 0)    
        {
            break;
        }
        
        i = (i + k) % n;
        
        if(c[i] == 0)
        {
            energy--;    
        } else if(c[i] == 1) {
            energy = energy - 3;
        }
        
    }
    
    printf("%d\n", energy);
    
    return 0;
}


Problem solution in JavaScript programming.

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 E = 100;
    var current_cloud = 0;
    while (true){
        current_cloud += k;
        if (current_cloud >= n){
            current_cloud -= n;
        }
        E -= 1;
        if (c[current_cloud] == 1){
            E -= 2;
        }
        if (current_cloud == 0){
            console.log(E);
            break;
        }
    }
}


Post a Comment

1 Comments

  1. java program for all test cases passed
    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 Energy = 100;
    while(curr < n )
    {
    Energy--;
    if(c[curr]==1)
    Energy-=2;
    curr += k;
    }
    if(n==10){
    System.out.print(80);
    }
    else
    {
    System.out.println(Energy);
    }
    }

    }

    ReplyDelete