The Hurdle Race -ALGORITHM
The Hurdle Race
Dan is playing a video game in which his character competes in a hurdle race by jumping over hurdles with heights . He can initially jump a maximum height of units, but he has an unlimited supply of magic beverages that help him jump higher! Each time Dan drinks a magic beverage, the maximum height he can jump during the race increases by unit.
Given , , and the heights of all the hurdles, find and print the minimum number of magic beverages Dan must drink to complete the race.
Input Format
The first line contains two space-separated integers describing the respective values of (the number of hurdles) and (the maximum height he can jump without consuming any beverages).
The second line contains space-separated integers describing the respective values of .
Constraints
Output Format
Print an integer denoting the minimum number of magic beverages Dan must drink to complete the hurdle race.
Sample Input 0
5 4
1 6 3 5 2
Sample Output 0
2
Explanation 0
Dan's character can jump a maximum of units, but the tallest hurdle has a height of :
To be able to jump all the hurdles, Dan must drink magic beverages.
Sample Input 1
5 7
2 5 4 5 2
Sample Output 1
0
Explanation 1
Dan's character can jump a maximum of units, which is enough to cross all the hurdles:
image
Because he can already jump all the hurdles, Dan needs to drink magic beverages.
package hacker_magichurdle;
import java.util.*;
public class Hacker_magichurdle {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int result=0;
int[] height = new int[n];
for(int height_i=0; height_i < n; height_i++){
height[height_i] = in.nextInt();
}
Arrays.sort(height);
if(height[n-1]>k)
{
result=height[n-1]-k;
}
System.out.println(result);
}
}
0 comments:
Post a Comment