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.
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 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 where 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 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.
Example
height=[1,2,3,3,2]
k=1
def doses_needed(max_jump_height, hurdles):
max_hurdle_height = max(hurdles)
doses_required = max_hurdle_height - max_jump_height
return max(0, doses_required)
# Example usage:
max_jump_height = 5
hurdles = [2, 5, 4, 8, 9]
result = doses_needed(max_jump_height, hurdles)
print("Doses needed:", result)
- Finding the Maximum Hurdle Height: First, we find the maximum height among all the hurdles. This gives us the height of the tallest hurdle that the character needs to clear.
- Calculating Required Doses: We then calculate the difference between the maximum hurdle height and the character’s current maximum jump height. This difference represents how much higher the character needs to be able to jump in order to clear the tallest hurdle.
- Handling Negative Differences: If the character’s maximum jump height is already greater than or equal to the maximum hurdle height, then they can already clear all the hurdles without needing any doses of the potion. In this case, the difference would be negative. So, we use the
max(0, doses_required)function to ensure that we don’t return a negative value for doses required. If the difference is negative, we return 0 doses required. - Returning the Result: Finally, we return the calculated doses required. If the character needs to increase their maximum jump height to clear the hurdles, this value will be positive and represent the number of doses needed. If the character can already clear all the hurdles, the function will return 0.