18
Jan
2020

Hackerrank Angry Professor (C++)

C++ :

string angryProfessor(int k, vector<int> a) {
    string cancelled = "YES";
    int onTime = 0;

    for(int i = 0; i < a.size(); i++){
       if(a[i] <= 0){
           onTime++;
       }
    }

    if(onTime >= k){
        cancelled = "NO";
    }

    return cancelled;
}

Explanation:

This one is simply. Set the return string cancelled to “YES” as default, and number of on time student onTime to 0. For each element in vector a, if the student arrived earlier or on time (0 or < 0), increment the number of students onTime by 1. At the end of the for loop, if the number of students on time is greater than or equals to the threshold number of students, set cancelled to “NO”.

You may also like...

Leave a Reply

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