Problem overview
Version 5 works with a catalog that covers several products. Three parallel vectors describe each row: products, brands and ratings. The variable target names the product of interest, here "Laptop", and k is 2. The program keeps only the rows for that product, then prints the top k brands by rating, highest first, as rank. Brand (rating). Ties go to the entry listed first.
Three of the six rows are laptops: TechPro (4), NeoByte (5) and AlphaTech (4). The top two are:
1. NeoByte (5)
2. TechPro (4)
If fewer than k products match, the program lists only the ones that exist, with no extra lines and no error. The previous version ranked every brand with a repeated search, and this one runs the same search on a filtered copy of the data.
Approach: filter into new lists, then reuse the search
Filtering is the first phase. Walk through every row, and whenever products[i] equals target, copy that row's brand and rating into two new vectors, filteredBrands and filteredRatings. Adding to both vectors in the same order keeps them parallel: a brand and its rating end up at the same position in the new lists.
Ranking is the second phase, and it is the repeated search from the previous version, with two changes: it reads the filtered vectors, and it must cope with fewer than k candidates. In each round it finds the best brand that isn't marked as used, using a strict comparison so that ties stay with the earlier entry. Then it marks that brand, adds a line, and starts the next round.
For the starter data, the first phase leaves these lists, in the original order:
filteredBrands: TechPro, NeoByte, AlphaTech
filteredRatings: 4, 5, 4
The second phase sees only these three entries, so a round can never pick a phone or a tablet.
Step by step
- Create
filteredBrandsas an emptyvector<string>andfilteredRatingsas an emptyvector<int>. - Loop over the rows with
ithrough every position ofproducts, since all three vectors line up. - Test the product: an
ifchecksproducts[i] == target. Forstringvalues,==compares the contents, so this is the correct test. - Copy the row with
push_backon both new vectors, in the same order. - Create
usedwith onefalsefor each filtered rating, and an emptyvector<string> lines. - Write the outer loop with
rankfrom 1 tokinclusive, and start each round withbestIndex = -1. - Write the inner loop over the positions of
filteredRatings. Skip entries withused[i]true, and pick an entry whenbestIndexis -1 or its rating is strictly higher than that ofbestIndex. - Guard the result with
if (bestIndex != -1). When fewer thankbrands exist, the late rounds find nobody and add nothing. - Inside the guard, mark
used[bestIndex]and add the line, usingto_stringfor the rank and the rating. - Print all lines in a final loop.
The solution in C++
vector<string> products = {"Laptop", "Phone", "Laptop", "Tablet", "Phone", "Laptop"};
vector<string> brands = {"TechPro", "MegaCorp", "NeoByte", "Quantum", "Zenith", "AlphaTech"};
vector<int> ratings = {4, 5, 5, 3, 4, 4};
string target = "Laptop";
int k = 2;
vector<string> filteredBrands;
vector<int> filteredRatings;
for (int i = 0; i < (int)products.size(); i++) {
if (products[i] == target) {
filteredBrands.push_back(brands[i]);
filteredRatings.push_back(ratings[i]);
}
}
vector<bool> used(filteredRatings.size(), false);
vector<string> lines;
for (int rank = 1; rank <= k; rank++) {
int bestIndex = -1;
for (int i = 0; i < (int)filteredRatings.size(); i++) {
if (!used[i]) {
if (bestIndex == -1 || filteredRatings[i] > filteredRatings[bestIndex]) {
bestIndex = i;
}
}
}
if (bestIndex != -1) {
used[bestIndex] = true;
lines.push_back(to_string(rank) + ". " + filteredBrands[bestIndex] + " (" + to_string(filteredRatings[bestIndex]) + ")");
}
}
for (int i = 0; i < (int)lines.size(); i++) cout << lines[i] << endl;
The search loops run over filteredRatings, and used has one flag per filtered entry. A position in the filtered lists is not a position in the full catalog. NeoByte is at position 2 in the catalog and at position 1 in the filtered lists, so mixing the two would pick the wrong brand. As written, the second phase never touches products, brands or ratings.
The casts such as (int)products.size() in the loop headers make both sides of each comparison signed, because size() returns an unsigned value. They are harmless here and avoid surprises if an expression ever subtracts from a size.
Complexity
Filtering is one pass over n rows. If m rows match, the ranking takes O(k × m). The total is O(n + k × m), and the extra space is O(m) for the filtered lists and flags, plus O(k) for the lines.
Edge cases and common mistakes
- Fewer matches than
k. For"Tablet"andk = 2, only one row matches, so the output is the single line1. Quantum (3). Without thebestIndex != -1check, the second round would writeused[-1]. - No match at all. For a product that isn't in the catalog, such as
"Watch", the filtered vectors stay empty and the program prints nothing. - A larger
kon laptops. Withk = 5the output is the three laptops: NeoByte (5), TechPro (4) and AlphaTech (4). - Ties. TechPro and AlphaTech both have 4. TechPro is listed first, so it ranks higher.
- Another product. With
"Phone"andk = 2you get1. MegaCorp (5)and2. Zenith (4). - Copying only one vector. If you push the brand but forget the rating, the vectors drift apart and the results are wrong or out of range.
Testing
The starter data prints the two lines above. Change target to "Phone" or "Tablet", or set k to 5, and compare the output with the cases above.
