Start learning free

LESSON 142 OF 410 · LOOPS & GLOBAL VARIABLES

Student Grades in Java (Time Complexity: O(n))

Practice Read the full explanation

Given the grades of a student as an array, determine if the student has passed the class.

A Student has passed the class if the average of grades is 5 or more.

The average is defined as (grades[0] + grades[1] + ... + grades[n - 1]) / n

Example 1:

Input: grades = [4, 7, 5, 9, 8, 2]
Output: true
Explanation:
average = (4 + 7 + 5 + 9 + 8 + 2) / 6 =
= 35 / 6 = 5.833333333... >= 5

Example 2:

Input: grades = [4, 7, 5, 3, 8, 2]
Output: false
Explanation:
average = (4 + 7 + 5 + 3 + 8 + 2) / 6 =
= 29 / 6 = 4.83333.. < 5

Hints:

The problem can be broken in 3 steps:
1. Compute the sum of the array
2. Compute the average by dividing the sum by the length of the array
3. Decide the result based on the average being greater than or equal to 5
Let's start with step 1: How can we compute the sum of the array?

First of all, we should store this sum somewhere as we compute it. That 'somewhere' is a variable, let's name it sum. We have to iterate through every element of grades and add its value to sum. How can we achieve this?

With a loop! Let's use a for loop and a variable grade to iterate through the elements:
sum = 0
for each grade in grades:
	sum += grade
When the loop terminates, we know the sum. What should we do next?

We should compute average = sum / length and return average >= 5:
sum = 0
for each grade in grades:
	sum += grade
average = sum / length
return average >= 5


We have a for loop that performs exactly n iterations (n is the length of grades), thus the Time Complexity is O(n).
The only extra space we use are the variables sum, average and the iterator variable grade inside the loop, thus the Extra Space used is O(1).
STEP 1 OF 5
    Ctrl + Enter to check

    JAVA · LOOPS & GLOBAL VARIABLES

    Student Grades in Java, explained

    Understanding the Problem

    The core challenge of this problem is to determine if the average of a student's grades meets or exceeds a threshold of 5. This is a common problem in educational software where determining pass/fail status based on grades is essential.

    Potential pitfalls include not correctly calculating the sum or average, especially with integer division in some programming languages. Misunderstanding the problem requirements, such as the threshold value, can also lead to incorrect results.

    Approach

    To solve this problem, we can break it down into three main steps:

    1. Compute the sum of the grades array.
    2. Compute the average by dividing the sum by the length of the array.
    3. Check if the average is greater than or equal to 5.

    Let's start with a naive solution and then discuss why it is optimal for this problem.

    Naive Solution

    The naive solution involves iterating through the array to compute the sum, then dividing by the number of elements to get the average. This approach is straightforward and efficient for this problem.

    Optimized Solution

    Given the simplicity of the problem, the naive solution is already optimal. The time complexity is O(n) because we need to iterate through all n elements of the array to compute the sum. The space complexity is O(1) as we only use a few extra variables.

    Algorithm

    Here is a step-by-step breakdown of the algorithm:

    1. Initialize a variable sum to 0.
    2. Iterate through each grade in the array and add it to sum.
    3. Compute the average by dividing sum by the length of the array.
    4. Return true if the average is greater than or equal to 5, otherwise return false.

    Code Implementation

    public class StudentGrades {
        public static boolean hasPassed(int[] grades) {
            // Step 1: Compute the sum of the grades
            int sum = 0;
            for (int grade : grades) {
                sum += grade;
            }
            
            // Step 2: Compute the average
            double average = (double) sum / grades.length;
            
            // Step 3: Check if the average is greater than or equal to 5
            return average >= 5;
        }
    
        public static void main(String[] args) {
            int[] grades1 = {4, 7, 5, 9, 8, 2};
            int[] grades2 = {4, 7, 5, 3, 8, 2};
            
            System.out.println(hasPassed(grades1)); // Output: true
            System.out.println(hasPassed(grades2)); // Output: false
        }
    }
    

    Complexity Analysis

    The time complexity of this solution is O(n) because we iterate through the array once to compute the sum. The space complexity is O(1) because we only use a few extra variables regardless of the input size.

    Edge Cases

    Potential edge cases include:

    • An empty array: This should be handled by checking if the array is empty before computing the average.
    • All grades are the same: The algorithm should still correctly compute the average.
    • Grades with decimal values: Ensure the average calculation handles floating-point division correctly.

    Testing

    To test the solution comprehensively, consider the following test cases:

    • Simple cases with a few grades.
    • Edge cases such as an empty array or all grades being the same.
    • Cases where the average is exactly 5.

    Using a testing framework like JUnit can help automate and manage these tests effectively.

    Thinking and Problem-Solving Tips

    When approaching such problems, break them down into smaller steps and solve each step methodically. Practice similar problems to improve your problem-solving skills and understand different algorithms and their applications.

    Conclusion

    In this blog post, we discussed how to determine if a student has passed based on their grades. We broke down the problem, discussed a straightforward approach, and provided a detailed Java implementation. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free