The Hurdle Race HackerRank Solution in C, C++, Java, Python

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  and , the number of hurdles and the maximum height the character can jump naturally.

The second line contains  space-separated integers  where .

Constraints

  • 1<=n, k<=100
  • 1<=height[i]<=100

Sample Input 0

5 4

1 6 3 5 2

Sample Output 0

2

 

The Hurdle Race HackerRank Solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n; 
    int k; 
    int max=-1,ans=0;
    scanf("%d %d",&n,&k);
    int *height = malloc(sizeof(int) * n);
    for(int height_i = 0; height_i < n; height_i++){
       scanf("%d",&height[height_i]);
        if(height[height_i]>max)
            max=height[height_i];
    }
    if(max>k)
        ans=max-k;
    printf("%d",ans);
    // your code goes here
    return 0;
}

 

The Hurdle Race HackerRank Solution in 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);
    // your code goes here
    return 0;
}

 

The Hurdle Race HackerRank Solution in 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];
        for(int height_i=0; height_i < n; height_i++){
            height[height_i] = in.nextInt();
        }
        Arrays.sort(height);
        System.out.println(Math.max(0, height[n-1]-k));
    }
}

 

The Hurdle Race HackerRank Solution in Python

#!/bin/python

import sys


n,k = raw_input().strip().split(' ')
n,k = [int(n),int(k)]
height = map(int, raw_input().strip().split(' '))
# your code goes here
m = max(height)
print max(0,m-k)

 

The Hurdle Race HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static void Main(String[] args) {
        string[] tokens_n = Console.ReadLine().Split(' ');
        int n = Convert.ToInt32(tokens_n[0]);
        int k = Convert.ToInt32(tokens_n[1]);
        string[] height_temp = Console.ReadLine().Split(' ');
        int[] height = Array.ConvertAll(height_temp,Int32.Parse);
        // your code goes here
        
        var max = height.Max();
        var diff = Math.Max(max - k, 0);
        Console.WriteLine(diff);
    }
}

 

 

Attempt –  HackerRank Challenge

Link – https://www.hackerrank.com/challenges/the-hurdle-race/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/designer-pdf-viewer-hackerrank/

Leave a Comment