PROBLEM LINK:
Author: Egor Bobyk
Tester: Mugurel Ionut Andreica
Editorialist: Praveen Dhinwa
DIFFICULTY:
Cakewalk
PREREQUISITES:
basic knowledge of programming in any language
PROBLEM:
You are given grade points of a student in total n courses. Grade points of a student can be between 2 to 5. A grade point of 2 means that student has failed the course.
A student can receive scholarship if he satisfies all of the below given conditions.
- He/She has not failed any course.
- His/Her average grade point is not less than 4.
- There should be at least one course in which he got perfect grade points, (i.e. a grade point of 5).
You have to tell whether the student will be able to receive scholarship or not.
EXPLANATION:
This is a simple implementation problem. You can check all the conditions easily. See the below given pseudo codes for checking various conditions.
He/She has not failed any course.
failed = false;
for i = 1 to n:
if score[i] == 2:
failed = true;
if (failed) {
willGetScholarship = false;
}
His/Her average grade point is not less than 4.
As we know the average grade point will be sum of all grades divided by n. There are two ways of checking this condition.
Method 1
int totalScore = 0;
for i = 1 to n:
totalScore += score[i]
double averageScore = totalScore / n;
if (averageScore < 4.0) {
willGetScholarship = false;
}
Note that in this method, averageScore is computed in double
data type. Whenever possible, you should try to avoid comparison between two doubles due to inability of double representation to store all digits after decimal. You can write the above code in the following way in order to have all of your intermediate calculations in int
data type.
int totalScore = 0;
for i = 1 to n:
totalScore += score[i]
if (totaScore < 4 * n) {
willGetScholarship = false;
}
There should be at least one course in which he got perfect grade points, (i.e. a grade point of 5).
gotPerfectScore = false;
for i = 1 to n:
if score[i] == 5:
gotPerfectScore = true;
if (!gotPerfectScore) {
willGetScholarship = false;
}
In this way, you can check all the conditions using a single pass over the array representing the grade points in all the courses. Time complexity of this algorithm will be \mathcal{O}(n) where n denotes number of courses.