# The Hurdle Race | HackerRank Solution

Hello coders, today we are going to solve The Hurdle Race HackerRank Solution which is a Part of HackerRank Algorithm Series.

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

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.

## Solution – The Hurdle Race

### C++

```#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);
return 0;
}
```

### Python

```import sys

n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
height = list(map(int, input().strip().split(' ')))
print(max(0, max(height) - k))```

### 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];
int max = 0;
for(int height_i=0; height_i < n; height_i++){
height[height_i] = in.nextInt();
max = Math.max(max, height[height_i]);
}
System.out.println(Math.max(0, max - k));
// your code goes here
}
}```

Disclaimer: The above Problem (The Hurdle Race) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.