Hello Programmers, In this post, you will know how to solve the HackerRank The Hurdle Race Solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank The Hurdle Race Solution
HackerRank The Hurdle Race Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank The Hurdle Race Solution

Task

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 1 = 2 doses of potion to be able to jump all of the hurdles.

Function Description

Complete the hurdleRace function in the editor below.

hurdleRace 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 integers height[i] where 0 <= i < n.

Constraints

  • 1 <= n, k <= 100
  • 1 <= height[i] <= 100

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 h1 = 6:
To be able to jump all the hurdles, Dan must drink 6 – 4 = 2 doses.

image

To be able to jump all the hurdles, Dan must drink 64=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:

image

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

HackerRank The Hurdle Race Solution

The Hurdle Race Solution in 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; 
    int k; 
    int max=-1,ans=0;
    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]);
        if(height[height_i]>max)
            max=height[height_i];
    }
    if(max>k)
        ans=max-k;
    printf("%d",ans);
    // your code goes here
    return 0;
}

The Hurdle Race Solution in Cpp

#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> height(n);
    int maxall=0;
    for(int height_i = 0; height_i < n; height_i++){
       cin >> height[height_i];
        maxall=max(maxall,height[height_i]);
    }
    cout<<max(0,maxall-k);
    // your code goes here
    return 0;
}

The Hurdle Race Solution in Java

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[] height = new int[n];
        for(int height_i=0; height_i < n; height_i++){
            height[height_i] = in.nextInt();
        }
        Arrays.sort(height);
        System.out.println(Math.max(0, height[n-1]-k));
    }
}

The Hurdle Race Solution in Python

import sys
n,k = raw_input().strip().split(' ')
n,k = [int(n),int(k)]
height = map(int, raw_input().strip().split(' '))
# your code goes here
m = max(height)
print max(0,m-k)

The Hurdle Race Solution using JavaScript

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]);
    height = readLine().split(' ');
    height = height.map(Number);
    console.log(Math.max(0,Math.max(...height) -k)) 
    // your code goes here
}

The Hurdle Race Solution in Scala

import scala.io.StdIn.{readLine, readInt, readLong, readDouble}
import scala.io.Source.{stdin}
import scala.math.{abs}
object Solution {
    def main(args: Array[String]) {
        var inVals = readLine.split(" ").map(_.toInt)
        var K = inVals(1)
        var inMax = readLine.split(" ").map(_.toInt).toArray.max
        if(K >= inMax) println(0)
        else println(inMax - K)
    }
}

The Hurdle Race Solution in Pascal

VAR n,k,max,i:integer; a:array[1..100] of integer;
Begin
Readln(n,k);
for i:=1 to n do
Begin
Read(a[i]);
end;
max:=a[1];
for i:=2 to n do
Begin
if a[i] > max then max:=a[i];
end;
if k > max then Writeln('0') else Writeln(max - k);
Readln;
END.

Disclaimer: This problem (The Hurdle Race) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Utopian Tree Solution

Leave a Reply

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