Skip to content
  • Home
  • Contact Us
  • About Us
  • Privacy Policy
  • DMCA
  • Linkedin
  • Pinterest
  • Facebook
thecscience

TheCScience

TheCScience is a blog that publishes daily tutorials and guides on engineering subjects and everything that related to computer science and technology

  • Home
  • Human values
  • Microprocessor
  • Digital communication
  • Linux
  • outsystems guide
  • Toggle search form
HackerRank The Hurdle Race Problem Solution

HackerRank The Hurdle Race Problem Solution

Posted on April 14, 2023April 14, 2023 By Yashwant Parihar No Comments on HackerRank The Hurdle Race Problem Solution

In this post, We are going to solve HackerRank The Hurdle Race
Problem. A video player plays a game in which the character competes in a hurdle race. Hurdles are of varying heights, and the characters have a maximum height they can jump. There is a magic potion they can take that will increase their maximum jump height by 1 unit for each dose. How many doses of the potion must the character take to be able to jump all of the hurdles? If the character can already clear all of the hurdles, return 0.

Example

height = [1, 2, 3, 3, 2]

k = 1

The character can jump 1 unit high initially and must take 3-2 = 1 doses of potion to be able to jump all of the hurdles.

Function Description

Complete the hurdle race function in the editor below.

hurdle race has the following parameter(s):

  • int k: the height the character can jump naturally
  • int height[n]: the heights of each hurdle

Returns

  • int: the minimum number of doses required, always 0 or more

Input Format

The first line contains two space-separated integers n and k, the number of hurdles and the maximum height the character can jump naturally.
The second line contains n space-separated height [I] integers where 0 < I < n.

Sample Input 0

5 4
1 6 3 5 2

Sample Output 0

2

Explanation 0

Dan’s character can jump a maximum of k = 4 units, but the tallest hurdle has a height of : h = 6

To be able to jump all the hurdles, Dan must drink 6-4 = 2 doses.

Sample Input 1

5 7
2 5 4 5 2

Sample Output 1

0

Explanation 1

Dan’s character can jump a maximum of k = 7 units, which is enough to cross all the hurdles:

Because he can already jump all the hurdles, Dan needs to drink 0 doses.

HackerRank The Hurdle Race Problem Solution
HackerRank The Hurdle Race Problem Solution

Table of Contents

  • The Hurdle Race C Solution
  • The Hurdle Race C++ Solution
  • The Hurdle Race C Sharp Solution
  • The Hurdle Race Java Solution
  • The Hurdle Race JavaScript Solution
  • The Hurdle Race Python Solution

The Hurdle Race C Solution

#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 *height = malloc(sizeof(int) * n);
    for(int height_i = 0; height_i < n; height_i++){
       scanf("%d",&height[height_i]);
    }
    int max;
    int count = 0;
    for(int i = 0; i < n; i++){
        if(height[i] > k){
            count += height[i]-k;
            k = height[i];
        }
    }
    printf("%d",count);
    return 0;
}

The Hurdle Race C++ Solution

#include <bits/stdc++.h>

using namespace std;

int main ()
{
	int n,k;
	cin >> n >> k;
	int r = 0;
	for (int i = 0; i < n;i++)
	{
		int t;
		cin >> t;
		r = max(r,max(0,t-k));
	}
	cout << r << endl;
}

The Hurdle Race C Sharp Solution

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

    static void Main(String[] args) {
        string[] tokens_n = Console.ReadLine().Split(' ');
        int n = Convert.ToInt32(tokens_n[0]);
        int k = Convert.ToInt32(tokens_n[1]);
        string[] height_temp = Console.ReadLine().Split(' ');
        int[] height = Array.ConvertAll(height_temp,Int32.Parse);
        // your code goes here
        var drinkCount = 0;
        for (int i = 0; i < n; i++) {
            if (k + drinkCount < height[i]) {
                drinkCount += height[i] - (k + drinkCount);
            }
        }
        
        Console.WriteLine(drinkCount);
    }
}

The Hurdle Race Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'hurdleRace' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER k
     *  2. INTEGER_ARRAY height
     */

    public static int hurdleRace(int k, List<Integer> height) {
        int max = Collections.max(height);
        
        return max - k > 0 ? max - k : 0;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        int n = Integer.parseInt(firstMultipleInput[0]);

        int k = Integer.parseInt(firstMultipleInput[1]);

        List<Integer> height = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        int result = Result.hurdleRace(k, height);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

The Hurdle Race JavaScript Solution

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() {
    const paramsInput = readLine().split(' ');
    const numHurdles = parseInt(paramsInput[0]);
    const startJumpHeight = parseInt(paramsInput[1]);
    const rawHeights = readLine().split(' ');
    const heights = rawHeights.map(Number);
    // your code goes here

    // Find the maximum value
    let maxHurdleHeight = 0;
    heights.forEach((value) => {
       if (value > maxHurdleHeight) {
           maxHurdleHeight = value;
       } 
    });
    
    // Drinking a potion gains 1 jump height so we just need the
    // difference between the current jump and maximum hurdle height.
    // If the max hurdle height is less that the current jump, not potions are needed.
    const potionsToDrink = maxHurdleHeight > startJumpHeight ? (maxHurdleHeight - startJumpHeight) : 0;
    console.log(potionsToDrink);
}

The Hurdle Race Python Solution

#!/bin/python3

import sys


n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
height = list(map(int, input().strip().split(' ')))

units = max(height) - k

if units > 0:
    print(units)
else:
    print(0)
   
    

Other Solutions

  • HackerRank Designer PDF Viewer Solution
  • HackerRank Utopian Tree Problem Solution
c, C#, C++, HackerRank Solutions, java, javascript, python Tags:C, cpp, CSharp, Hackerrank Solutions, java, javascript, python

Post navigation

Previous Post: HackerRank Climbing the Leaderboard Solution
Next Post: HackerRank Designer PDF Viewer Solution

Related Posts

HackerRank Fraudulent Activity Notifications Problem Solution HackerRank Fraudulent Activity Notifications c
HackerRank HackerX Problem Solution HackerRank HackerX Problem Solution c
HackerRank Similar Strings Problem Solution HackerRank Similar Strings Problem Solution c
HackerRank Divisible Sum Pairs Problem Solution HackerRank Divisible Sum Pairs Problem Solution c
HackerRank Running Time of Algorithms Problem Solution HackerRank Running Time of Algorithms Solution c
Add an Custom Snippet for Javascript in Visual Studio Code developer guide

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick Your Subject
Human Values

Copyright © 2023 TheCScience.

Powered by PressBook Grid Blogs theme