Problem overview
Many brands now sell the same product. The data is stored in two parallel arrays: ratings[i] is the rating of brands[i]. The program finds the highest-rated brand by scanning the arrays with a loop and prints it in the form 1. Brand (rating).
The starter code has:
| index | brand | rating |
|---|---|---|
| 0 | TechPro | 4 |
| 1 | MegaCorp | 5 |
| 2 | Quantum | 3 |
| 3 | NeoByte | 5 |
| 4 | Zenith | 4 |
The highest rating is 5, shared by MegaCorp and NeoByte. If brands tie for the top rating, the one listed first wins, so the output is:
1. MegaCorp (5)
Approach
Version 2 compared two brands with an if/else, which can't scale to any number of brands. A loop can. Keep the position of the best brand found so far, in bestIndex, and check every other brand against it. When one has a strictly higher rating, it becomes the new best.
Storing the position, not the rating, is the key choice. One position gives the brand and the rating, because the two arrays line up. So the program doesn't need separate variables for the best name and the best rating, as the previous version did.
Before any comparison the first brand is the only one seen, so it is the best so far, and bestIndex starts at 0. The loop starts at position 1, because comparing the first brand with itself would do nothing.
Step by step
- Create
bestIndexas avarset to 0, since the loop will change it. - Write the loop.
for (i in 1 until ratings.size)makesirun from 1 up to, but not including, the number of ratings. It visits the same position in both arrays. - Test the current brand. Inside the loop, write an
ifthat checksratings[i] > ratings[bestIndex]. Strictly greater means that on a tie the earlier brand keeps the lead. - Update the best. Inside the
if, setbestIndex = i. - Print after the loop. Join
"1. ",brands[bestIndex]," (",ratings[bestIndex]and")".
The solution in Kotlin
val brands = arrayOf("TechPro", "MegaCorp", "Quantum", "NeoByte", "Zenith")
val ratings = intArrayOf(4, 5, 3, 5, 4)
var bestIndex = 0
for (i in 1 until ratings.size) {
if (ratings[i] > ratings[bestIndex]) {
bestIndex = i
}
}
println("1. " + brands[bestIndex] + " (" + ratings[bestIndex] + ")")
How the loop runs
- Start:
bestIndexis 0 (TechPro, rating 4). i = 1: rating 5 is greater than 4, sobestIndexbecomes 1.i = 2: rating 3 is not greater than 5.i = 3: rating 5 is not strictly greater than 5, sobestIndexstays 1. This is why MegaCorp beats NeoByte.i = 4: rating 4 is not greater than 5.
The loop ends with bestIndex equal to 1, and the program prints 1. MegaCorp (5).
Kotlin details
- Two array types.
arrayOf(...)makes anArray<String>, andintArrayOf(...)makes anIntArrayof primitive ints. Both are read with[i]and have asize. untilexcludes the end.1 until ratings.sizeis 1, 2, 3, 4 for five ratings. The range1..ratings.sizeincludes 5, andratings[5]throwsArrayIndexOutOfBoundsException.- The loop variable.
iis declared by theforitself and is a read-onlyval. You can't assign to it inside the loop, which is why the program copies it intobestIndex. - Brackets in the output.
brands[bestIndex]andratings[bestIndex]sit in the middle of a+chain. TheStringon the left makes+convert theIntrating to text.
Complexity
The loop checks each brand once, so the time is O(n) for n brands. The extra space is O(1): a single index.
Edge cases and common mistakes
- Ties. With
>=, the later brand would replace the earlier one on every tie, and the program would print NeoByte. The strict>is what the task requires. - The best is first. With ratings 5, 3, 4 for brands A, B, C, nothing beats position 0, and the output is
1. A (5). - The best is last. With ratings 2, 3, 5 the index moves at positions 1 and 2, and the output is
1. C (5). - A single brand. With one brand,
1 until 1is empty and the loop never runs. The program prints that brand: ratings of 2 for one brand named Only gives1. Only (2). - Remembering the rating, not the position. If you keep only the best rating, you can print the rating but can't tell which brand it belongs to.
- Going past the end. Use
untilwithratings.size.
Testing
| brands, ratings | output |
|---|---|
| the lesson's five brands, 4 5 3 5 4 | 1. MegaCorp (5) |
| A B C, 5 3 4 | 1. A (5) |
| A B C, 2 3 5 | 1. C (5) |
| Only, 2 | 1. Only (2) |
