09
Jan
2020

Hackerrank Breaking the Records (C++)

C++ :

vector<int> breakingRecords(vector<int> scores) {
    int lowest = scores[0], highest = scores[0], lowestBroken = 0, highestBroken = 0;
    vector<int> breakingScores;
    for(int i = 1; i < scores.size(); i++){
        if(scores[i] > highest){
            highest = scores[i];
            highestBroken++;
        }else if (scores[i] < lowest) {
            lowest = scores[i];
            lowestBroken++;
        }
    }
    breakingScores.push_back(highestBroken);
    breakingScores.push_back(lowestBroken);

    return breakingScores;
}

Explanation:

This one is simple. First, set both lowest and highest score to the first element of vector scores. Next, in the for loop, check if each element is higher than the highest score or lowest than the lowest score. If yes, add 1 to highestBroken and lowestBroken respectively.

Push poth highestBroken and lowestBroken into vector breakingScores, and return it.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *