loading...

Daily HackerRank Challenge - Day 13

wingkwong profile image Wing-Kam ・2 min read

About

This is a series of Daily HackerRank Challenge. Each day I show the some solutions written in C++.


Breaking the Records

Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there.

For example, assume her scores for the season are represented in the array scores = [12,24,10,24] . Scores are in the same order as the games played. She would tabulate her results as follows:

                                 Count
Game  Score  Minimum  Maximum   Min Max
 0      12     12       12       0   0
 1      24     12       24       0   1
 2      10     10       24       1   1
 3      24     10       24       1   1

Given Maria's scores for a season, find and print the number of times she breaks her records for most and least points scored during the season.

Sample Input

9
10 5 20 20 4 5 2 25 1

Sample Output

2 4

Loop through each score and check if it is higher / lower the highest score / lowest score. If so, update the value and increase the corresponding counter.

Final Solution

int n,s,max,h,l,ans1=0,ans2=0;

int main()  
{ 
    FAST_INP;
    cin >> n;
    REP(i,n){
        cin >> s;
        if(i==0) {
            h=l=s;
            continue;
        }

        if(s>h){
            h=s;
            ans1++;
        }
        else if(s<l){
            l=s;
            ans2++;
        }
    }
    cout << ans1 << " " << ans2;
    return 0; 
} 

The Hurdle Race

Dan is playing a video game in which his character competes in a hurdle race. Hurdles are of varying heights, and Dan has a maximum height he can jump. There is a magic potion he can take that will increase his maximum height by 1 unit for each dose. How many doses of the potion must he take to be able to jump all of the hurdles.

Given an array of hurdle heights height, and an initial maximum height Dan can jump,k, determine the minimum number of doses Dan must take to be able to clear all the hurdles in the race.

For example, if height=[1,2,3,3,2] and Dan can jump 1 unit high naturally, he must take 3-1=2 doses of potion to be able to jump all of the hurdles.

Sample Input

5 4
1 6 3 5 2

Sample Output

2

Loop throught the input and find out the tallest hurdle max. If max is greater than k, return max-k. Otherwise, return 0.

Final Solution


int n,k,h;

int main()  
{ 
    FAST_INP;
    cin >> n >> k;
    int max=0;
    REP(i,n){
        cin >> h;
        if(h>max) max=h;
    }
    cout << (k>max?0:abs(k-max));
    return 0; 
} 

Complete Code

Check out the complete code via below links

Posted on by:

wingkwong profile

Wing-Kam

@wingkwong

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.

Discussion

pic
Editor guide