Problem overview
This version hands out bonuses to the two most productive employees, the best first and the second best after. The data is the same three parallel vectors as before: names, productivities and salaries, where index i in each describes one employee. The second best is the employee with the highest productivity among all the others. When productivities tie, the employee listed first ranks higher. Each bonus is 15% of the salary, rounded down, and each line is printed as name: $bonus.
With the starter data, Alice Chen has 150 and Mary Jane has 133, so the output is:
Alice Chen: $37500
Mary Jane: $50250
Note that the second line has the larger bonus. The ranking is by productivity, not by bonus, and Mary Jane's salary is higher than Alice Chen's.
Approach: find the best, then find the best of the rest
The previous version found the best employee with one scan. There is no need to sort for the top two. After the first scan, run a second scan that ignores the winner: whoever has the highest productivity among everyone else is second.
The second scan needs a starting point. In the first scan, employee 0 served as the initial best, but here employee 0 might be the overall winner and therefore not allowed. So secondIndex starts at -1, a value that is not a valid index and so can mean "nobody chosen yet". The condition for taking over has two parts: the candidate is not the best employee, and either nobody is chosen yet or the candidate's productivity is strictly greater than that of the current second.
The strict comparison keeps the tie rule. When two employees tie for second place, the first listed is chosen first and is never replaced by a later one with the same productivity.
Step by step
- Create
bestIndexat 0. It is the best employee known so far. - Write the first loop with
ifrom 1 whilei < (int)names.size(). Employee 0 is already the best candidate. - Update
bestIndexwhenproductivities[i]is strictly greater thanproductivities[bestIndex]. - Create
secondIndexat-1. No employee has that index, so it means nobody is second yet. - Write the second loop with
istarting at 0. The best employee could be anywhere in the list, including at index 0, so this loop begins at the start and skips the best employee inside the body. - Test each candidate. The
ifrequiresi != bestIndexand also requires that eithersecondIndex == -1orproductivities[i] > productivities[secondIndex]. When both hold, setsecondIndextoi. - Compute
bestBonusassalaries[bestIndex] * 15 / 100. - Compute
secondBonusthe same way fromsecondIndex. - Print the best employee as the name, a colon, a dollar sign and the bonus.
- Print the second best in the same form on the next line.
The solution in C++
vector<string> names = {"John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"};
vector<int> productivities = {83, 133, 97, 150, 120};
vector<int> salaries = {135000, 335000, 210000, 250000, 180000};
int bestIndex = 0;
for (int i = 1; i < (int)names.size(); i++) {
if (productivities[i] > productivities[bestIndex]) {
bestIndex = i;
}
}
int secondIndex = -1;
for (int i = 0; i < (int)names.size(); i++) {
if (i != bestIndex && (secondIndex == -1 || productivities[i] > productivities[secondIndex])) {
secondIndex = i;
}
}
int bestBonus = salaries[bestIndex] * 15 / 100;
int secondBonus = salaries[secondIndex] * 15 / 100;
cout << names[bestIndex] << ": $" << bestBonus << endl;
cout << names[secondIndex] << ": $" << secondBonus << endl;
Details worth a closer look:
- Order inside the condition. In
secondIndex == -1 || productivities[i] > productivities[secondIndex], the left side is checked first. When it is true, the||operator skips the right side, soproductivities[-1]is never evaluated. If you swapped the two parts, the first candidate would read the vector at index -1, which is undefined behaviour. - Precedence.
&&binds tighter than||, so the parentheses around the||part are needed. Without them, the condition would mean something different: employeei != bestIndexand nobody chosen, or the productivity test, which could pick the best employee. - The second loop reuses
i. Eachfordeclares its owni, which exists only inside that loop, so the two loops don't conflict.
Complexity
There are two passes over the n employees, so the time is O(n), and the extra space is O(1). Sorting would cost O(n log n) and is not needed for two results.
Edge cases and common mistakes
- Everyone tied. With names X, Y, Z, productivities 9, 9, 9 and salaries 100, 200, 300, X is best because no later employee is strictly greater. In the second loop X is skipped, Y is chosen because
secondIndexis -1, and Z does not replace Y. The output isX: $15andY: $30. - The best employee listed last. With productivities 1, 5 and 9, the best is at index 2. The second loop picks index 1. The output is
Z: $45thenY: $30. - Tie for best. With X, Y, Z at productivities 3, 9 and 9, the best is Y, the first of the two. Z, with the same productivity, is second.
- Fewer than two employees. With one employee, the second loop never finds a candidate, so
secondIndexstays -1 andnames[secondIndex]reads outside the vector, which is undefined behaviour. The lesson's data has several employees; a general version would check the size first. - Starting the second loop at 1. That would skip employee 0, who may be the second best.
- Forgetting the strict
>.>=would let a later tied employee replace an earlier one.
Testing
| Input | Output |
|---|---|
| the starter data | Alice Chen: $37500, then Mary Jane: $50250 |
| X, Y, Z with productivities 9, 9, 9 and salaries 100, 200, 300 | X: $15, then Y: $30 |
| X, Y with productivities 3, 8 and salaries 1000, 2000 | Y: $300, then X: $150 |
| X, Y, Z with productivities 1, 5, 9 and salaries 100, 200, 300 | Z: $45, then Y: $30 |
