Problem overview
Version 6 ranks the top k brands by rating, and uses the number of reviews to break ties. A third vector, reviews, sits next to brands and ratings. The rules are: a higher rating wins; if the ratings are equal, more reviews win; if both are equal, the brand listed first wins. Each line is rank. Brand (rating, reviews), and k is never larger than the number of brands.
With k = 3 the program prints:
1. NeoByte (5, 500)
2. MegaCorp (5, 150)
3. Zenith (4, 300)
MegaCorp and NeoByte both have rating 5, and NeoByte's 500 reviews beat 150. At rating 4, Zenith's 300 reviews beat TechPro's 200. The version builds on the repeated search from the fourth version of this program: the filtering is not part of it, and only the comparison gets a second criterion.
Approach: a comparison with two criteria
The repeated search stays as it is: each round finds the best brand that isn't taken, marks it, and adds a line. What changes is how "better" is decided. The rule has three parts, which fit an if / else if chain, and the order of the chain matters:
- If no brand has been found yet, take this one.
- Otherwise, if its rating is higher than the best one's, take it. Reviews don't matter at all here.
- Otherwise, if the ratings are equal and it has more reviews, take it.
Each case runs only when the ones above it were false. That is what lets the second case say simply "higher rating": a lower rating fails both the second and third cases, so the brand is not taken. Nothing is said about equal ratings with equal reviews, so the earlier brand stays, which is the rule for a full tie.
The chain is also easier to read than one long condition. A single expression would have to say "no best yet, or a higher rating, or an equal rating with more reviews", with parentheses to keep the groups straight. Three short cases, each with one idea, are easier to check against the rules one by one.
Step by step
- Create
usedwith onefalsefor each rating. - Create the empty
vector<string> lines. - Loop
rankfrom 1 tokinclusive. It counts the rounds and is the number printed first on each line. - Start the round with
bestIndex = -1. - Loop
iover every position ofratings. - Look only at brands that aren't taken:
if (!used[i]). - First case: when
bestIndexis -1, this brand is the best so far, so setbestIndex = i. - Second case:
else ifthe rating is higher than the best one's, it becomes the best. - Third case:
else ifthe ratings are equal and this brand has more reviews, it becomes the best. - Mark the winner with
used[bestIndex] = true. - Add the line with
push_back: rank,". ", name," (", rating,", ", reviews and")". Numbers go throughto_string. - Print every line in a final loop.
The solution in C++
vector<string> brands = {"TechPro", "MegaCorp", "NeoByte", "Quantum", "Zenith"};
vector<int> ratings = {4, 5, 5, 3, 4};
vector<int> reviews = {200, 150, 500, 100, 300};
int k = 3;
vector<bool> used(ratings.size(), false);
vector<string> lines;
for (int rank = 1; rank <= k; rank++) {
int bestIndex = -1;
for (int i = 0; i < (int)ratings.size(); i++) {
if (!used[i]) {
if (bestIndex == -1) {
bestIndex = i;
}
else if (ratings[i] > ratings[bestIndex]) {
bestIndex = i;
}
else if (ratings[i] == ratings[bestIndex] && reviews[i] > reviews[bestIndex]) {
bestIndex = i;
}
}
}
used[bestIndex] = true;
lines.push_back(to_string(rank) + ". " + brands[bestIndex] + " (" + to_string(ratings[bestIndex]) + ", " + to_string(reviews[bestIndex]) + ")");
}
for (int i = 0; i < (int)lines.size(); i++) cout << lines[i] << endl;
The two conditions in the third case are joined with &&: both must hold, the same rating and more reviews. Writing ratings[i] == ratings[bestIndex] || reviews[i] > reviews[bestIndex] would let a brand with a lower rating win on reviews alone.
The first case checks bestIndex == -1 before anything reads ratings[bestIndex]. Because the cases form a chain, the later ones are never evaluated while bestIndex is -1.
Following the rounds
| rank | winner | why |
|---|---|---|
| 1 | NeoByte (5, 500) | ties MegaCorp on rating, more reviews |
| 2 | MegaCorp (5, 150) | the only brand with rating 5 left |
| 3 | Zenith (4, 300) | ties TechPro on rating, more reviews |
Complexity
Each of the k rounds scans all n brands, so the time is O(k × n). The extra space is O(n) for used and O(k) for lines.
Edge cases and common mistakes
- Reviews deciding too early. If you compare reviews first, a brand with a lower rating but many reviews would beat a better-rated one. Rating decides first.
- A full tie. With ratings 5 and 5 and reviews 10 and 10, both comparisons are false for the later brand, so the earlier one wins and the lines come out in the listed order. The lesson's data has no such pair, but the code follows the rule. With reviews 10 and 20, the second brand comes first.
- All five brands. With
k = 5, the lines after the three above are4. TechPro (4, 200)and5. Quantum (3, 100). - Forgetting to mark the winner. The same brand would repeat on every line.
- A
klarger than the number of brands. The statement rules this out. In this version the extra round would leavebestIndexat -1 and the program would useused[-1], which is undefined behaviour.
Testing
The starter data prints the three lines above. Set k to 5 to see the full ranking, or change NeoByte's review count to 100. Then MegaCorp (150 reviews) takes the first line and NeoByte moves to the second.
