The Hurdle Race

Hacker Rank Solution


The Hurdle Race

Hey guys ,welcome to another blog on hacker rank solution. this time we gonna discuss a hacker rank algorithim problem, which is named as The Hurdle Race.








See the example



As it is very clear from the Example that ,to be able to cross all the hurdle , he should be able to jump the hurdle with the maximum height, so our first task is to find out the maximum of the array.
After this, we have to find how much beverage he should drink to be able to jump the max-hurdle.this can be evaluted by subtracting the max of the array from K.
Remember if K is equal or greater than max-hurdle ,he can jump all the hurdles without drinking any beverages, so we will print zero .

Here is the code in java.



import java.io.*;

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {


    static int hurdleRace(int k, int[] height) {

         int max=height[0];
        for(int i=1;i<height.length;i++)
         {
             if(height[i]>max)
                 max=height[i];
         }
     return (k>=max) ? 0 : max-k ;
    }

    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();
        }
        int result = hurdleRace(k, height);
        System.out.println(result);
        in.close();
    }

}

Follow me on social media:
Facebook , Instagram.

For any Questions regarding this or if you want me to explain any other problems ,do let me know in comment section.

Comments

Popular posts from this blog

Happy Ladybugs

Strange Counter