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.

The Hurdle Race

Contents

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.

Leave a Comment

Your email address will not be published.