Grading Students HackerRank Problem Solution

In this HackerRank Sparse Arrays problem, we need to follow the hackerrank university grading policy and develop a program. HackerLand University has the following grading policy:

Problem Solution in Python

def gradingStudents(grades):
    for i in range(len(grades)):
        if grades[i]<38:
            continue
        diff = 5 - grades[i] %5
        if diff<3:
            grades[i]+=diff


    return grades

Problem Solution in Java

List result = new ArrayList<>();
    for (Integer grade : grades) {
        int ost = grade % 5;
        int forRoundGrade = 5 - ost;


        if (grade < 38 || ost < 3) {
            result.add(grade);
        } else {
            result.add(grade + forRoundGrade);
        }
    }


    return result;

Problem Solution in C++

vector<int> gradingStudents(vector<int> grades) {
    for (unsigned short i = 0; i < grades.size(); ++i) {
        if (grades[i] < 38) continue;
        else {
            unsigned short grade = grades[i] % 5;
            if (grade > 2) grades[i] = grades[i] + (5 - grade);
        }
    }
    return grades;
}

Leave a Comment