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:
- Every student receives a in the inclusive range from to .
- Any less than is a failing grade.
Sam is a professor at the university and likes to round each student’s according to these rules:
- If the difference between the and the next multiple of is less than , round up to the next multiple of .
- If the value of is less than , no rounding occurs as the result will still be a failing grade.
Examples
- round to (85 – 84 is less than 3)
- do not round (result is less than 40)
- do not round (60 – 57 is 3 or higher)
Given the initial value of for each of Sam’s students, write code to automate the rounding process.
Function Description
Complete the function gradingStudents in the editor below.
gradingStudents has the following parameter(s):
- int grades[n]: the grades before rounding
Returns
- int[n]: the grades after rounding as appropriate
Input Format
The first line contains a single integer, , the number of students.
Each line of the subsequent lines contains a single integer, .
Constraints
Grading Students Hacker Rank Problem Solutions
I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.
Problem Solution in JavaScript
function gradingStudents(grades) {
for(let i=0; i< grades.length; i++){
if(grades[i] >= 38){
if((grades[i] + 1) % 5 === 0){
grades[i] = grades[i] +1;
}else if((grades[i] + 2) % 5 === 0){
grades[i] = grades[i] +2;
}
}
}
return grades;
}
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 Reply