Problem overview
This version gives bonuses to the top two employees, best first and second best next. The data is the same three parallel arrays as before: names, productivities and salaries, where one index describes one employee. Each bonus is 15% of that employee's salary, rounded down with integer division. The second best is the one with the highest productivity among all the other employees. On equal productivity, the one listed first wins. Each line has the form name: $bonus.
For the starter data, Alice Chen (150) is first and Mary Jane (133) is second:
Alice Chen: $37500
Mary Jane: $50250
The previous version found the best index with one loop. This version keeps that loop and adds a second one for the runner-up.
Approach: two passes, the second skipping the winner
You could sort the employees by productivity and take the first two, but sorting three parallel arrays together is awkward and unnecessary. Two scans do the job.
The first scan is the one already built: bestIndex starts at 0 and changes when someone has a strictly higher productivity. The second scan looks for the best employee except bestIndex. It starts from secondIndex = -1, which means "nobody chosen yet", since no employee has index -1. For each i, the employee qualifies if both hold:
i != bestIndex: the best employee already has first place.- Either nobody is chosen yet (
secondIndex == -1), or the productivity atiis strictly greater than the one atsecondIndex.
The -1 case matters because there is nothing to compare against for the first qualifying employee. The condition is written so that productivities[secondIndex] is never evaluated while secondIndex is -1: the || operator stops as soon as its left side is true. Without that, reading index -1 would throw an exception.
Step by step
- Find the best index. Set
bestIndexto 0 and loopifrom 1 whilei < names.Length, updatingbestIndexwhenproductivities[i]is strictly greater thanproductivities[bestIndex]. This is the same loop as in the last version. - Create
secondIndexand set it to -1. It marks "nobody yet". - Write a second loop from 0. It starts at 0 because the best employee can be anywhere in the list. They are skipped inside the loop instead of being excluded by the start.
- Inside it, write the
if. It requiresi != bestIndex, and eithersecondIndex == -1orproductivities[i] > productivities[secondIndex]. When all of that holds, setsecondIndex = i. - Compute
bestBonusandsecondBonusassalaries[index] * 15 / 100for each index. - Print the best employee as the name, a colon, a dollar sign and the bonus, then print the second best on the next line in the same form.
The solution in C#
string[] names = {"John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"};
int[] productivities = {83, 133, 97, 150, 120};
int[] salaries = {135000, 335000, 210000, 250000, 180000};
int bestIndex = 0;
for (int i = 1; i < names.Length; i++) {
if (productivities[i] > productivities[bestIndex]) {
bestIndex = i;
}
}
int secondIndex = -1;
for (int i = 0; i < names.Length; i++) {
if (i != bestIndex && (secondIndex == -1 || productivities[i] > productivities[secondIndex])) {
secondIndex = i;
}
}
int bestBonus = salaries[bestIndex] * 15 / 100;
int secondBonus = salaries[secondIndex] * 15 / 100;
Console.WriteLine(names[bestIndex] + ": $" + bestBonus);
Console.WriteLine(names[secondIndex] + ": $" + secondBonus);
Details worth knowing:
- Two loops, one variable name. Both
forloops declareint i. That is legal because the scope of a loop variable ends with its loop. Declaringia second time while the first is still in scope would be an error. - Short-circuit evaluation.
&&and||evaluate their right side only when needed. HeresecondIndex == -1 || productivities[i] > productivities[secondIndex]relies on that, as described above. - Precedence and parentheses.
&&binds tighter than||. The parentheses around the||part make the condition mean "not the best, and (nobody yet or better)". Without them it would mean something different. - A sentinel value.
-1works as "none" because valid indexes start at 0. The code that usessecondIndexafter the loop assumes it is no longer -1, which holds when the arrays have at least two employees.
Complexity
Time: O(n) for n employees: two passes, each doing constant work per element. Space: O(1), since only a few integers are stored. Sorting would cost O(n log n) and more code.
Edge cases and common mistakes
- Ties for the best score. With productivities 83, 150, 97, 150 and 120, employees 1 and 3 tie. The first loop picks employee 1 (Mary Jane) as best. The second loop skips her and finds Alice Chen. The output is
Mary Jane: $50250thenAlice Chen: $37500. - Ties for second. With 83, 133, 97, 150 and 133, employees 1 and 4 tie for second. Strictly greater keeps the earlier one: Mary Jane.
- The best employee is first or last. With productivities 200, 133, 97, 150 and 120, the output is John Doe, then Alice Chen. With 1, 2, 3, 4 and 5, it is Carlos Diaz, then Alice Chen. The skip works wherever the best employee sits.
- Only two employees. With productivities 5 and 9, the first loop picks index 1 and the second loop picks index 0.
- Only one employee. The second loop finds nobody,
secondIndexstays -1 andsalaries[secondIndex]throws anIndexOutOfRangeException. The lesson's data always has at least two. - Forgetting
i != bestIndex. The second loop would pick the best employee again and print the same person twice. - Starting the second loop at 1. If the best employee is at the end of the list, employee 0 would never be considered for second place.
Testing
- The starter data gives
Alice Chen: $37500thenMary Jane: $50250. - Productivities 1, 2, 3, 4, 5 give
Carlos Diaz: $27000thenAlice Chen: $37500. - Productivities 83, 150, 97, 150, 120 give
Mary Jane: $50250thenAlice Chen: $37500. - Two employees A and B with productivities 5 and 9 and salaries 100000 and 200000 give
B: $30000thenA: $15000.
