Problem overview
The function hasPassed receives an IntArray named grades and returns a Boolean. A student passes when the average of the grades is 5 or more. The average is the sum of the grades divided by how many there are.
In the first example, [4, 7, 5, 9, 8, 2] sums to 35 across 6 grades, so the average is about 5.83 and the answer is true. In the second, [4, 7, 5, 3, 8, 2] sums to 29, the average is about 4.83, and the answer is false.
Approach: sum, divide, compare
The task breaks into three small pieces. First, add up every grade. Second, divide the sum by the number of grades. Third, compare the average with the passing mark of 5 and return the result of that comparison.
Adding up an array needs a loop with a variable that remembers the total so far. The loop visits each grade exactly once, so the time is linear in the number of grades. Only a few variables are stored, so the extra space stays constant.
Step by step
- Declare the running sum as
var sum = 0f. It is avarbecause it changes on every pass, and thefsuffix makes it aFloatrather than anInt, so that the later division keeps its fractional part. - Loop over the grades with
for (grade in grades). The loop gives you each element in turn, and you don't need an index. - Add each grade to the sum with
sum += grade. Adding anIntto aFloatgives aFloat, and the assignment keepssumaFloat, so no conversion is needed. - Compute the average with
val average = sum / grades.size. Avalis enough because the average is assigned once.grades.sizeis the number of elements of an array. - Return the comparison
average >= 5. The expression is already aBoolean, so there is no need forifand two returns.
The solution in Kotlin
class Solution {
fun hasPassed(grades: IntArray): Boolean {
var sum = 0f
for (grade in grades) {
sum += grade
}
val average = sum / grades.size
return average >= 5
}
}
The Kotlin details behind it:
- Mixed arithmetic. Kotlin has no implicit conversion between number types in assignments, but arithmetic operators are overloaded for the mixed cases.
Float / Intdivides as floating point and returns aFloat. The comparisonaverage >= 5also accepts theIntliteral 5 next to aFloat. - Integer division truncates. With an
Intsum,35 / 6is 5, not 5.83. Here that happens not to change the answer, because for non-negative numbers, rounding down never moves a value across the whole number 5. Still, aFloataverage matches the definition in the statement, and it is the habit that keeps other averaging problems right. Floathas limited precision. It is a 32-bit type, and35f / 6prints as5.8333335. For grades this does not matter. For very large sums, or when you need more digits, useDouble, whose literal has no suffix:0.0.- An empty array. The sum is
0fandgrades.sizeis 0, so the division givesNaN(not a number) instead of an exception, as it does for integers. Every comparison withNaNis false, sohasPassedreturnsfalseforintArrayOf(). Intdivision by zero throws. If you had used anIntsum and divided bygrades.sizeof 0, Kotlin would throw anArithmeticException.
Complexity
Let n be the length of grades.
Time: O(n). The loop runs n times and does one addition per pass. The division and the comparison happen once.
Space: O(1). The function stores sum, average and the loop variable grade, and none of them grows with n.
Edge cases and common mistakes
- Exactly 5. The pass mark includes 5, so
intArrayOf(5)andintArrayOf(4, 6)both returntrue. Using>instead of>=would fail them. - Just below 5.
intArrayOf(4, 5)averages 4.5 and returnsfalse. - Dividing by the wrong number. Use
grades.size, not the largest indexgrades.size - 1. - Declaring the sum inside the loop. A new variable on every pass forgets the earlier grades.
- Using
valfor the sum. Avalcan't be reassigned, sosum += gradedoes not compile. - Writing
0instead of0f.var sum = 0makessumanInt, and thensum += gradewith aFloatwould not compile. Starting from0fkeeps the whole computation in floating point.
Testing
intArrayOf(4, 7, 5, 9, 8, 2)givestrue.intArrayOf(4, 7, 5, 3, 8, 2)givesfalse.intArrayOf(5)givestrue.intArrayOf(4, 6)givestrue.intArrayOf(4, 5)givesfalse.