Problem overview
The company gives bonuses to its k most productive employees, and k is a variable. Three parallel arrays describe the staff, and index i in all of them refers to the same person: names, productivities and salaries. The bonus is 15% of the salary, rounded down. The program prints one line per winner, best first, in the form name: $bonus, and it does not sort anything. Instead, it repeats the search for the most productive employee k times, skipping everybody who has already won.
With the starting data and k = 3 the output is:
Alice Chen: $37500
Mary Jane: $50250
Carlos Diaz: $27000
Alice has the highest productivity, 150, and her salary is 250000, so her bonus is 250000 * 15 / 100 = 37500. Mary is next with 133, and Carlos third with 120.
Approach: search k times, remembering the winners
A search for the best element is a loop with a "best so far" variable. Here that variable is an index, bestIndex, which starts at -1. No employee has index -1, so it means that nobody has been chosen yet. Each employee is compared with the current best, and replaces it only when their productivity is strictly greater. Strictly greater matters for ties: an employee who equals the current best does not replace them, so the one listed first wins.
To get the second best, the search must ignore the first winner. A bool array named taken, with one slot per employee, records who has already won. The search considers only employees whose slot is still false. After each round, the winner's slot is set to true, so the next round finds the best among the rest.
This is a nested loop: the outer loop counts the rounds, one for each winner, and the inner loop looks at every employee in each round. For 5 employees and k = 3 the inner body runs 15 times. The work grows as k times n, which is fine for small data and avoids the need to sort the arrays.
The last piece is the case where k is larger than the number of employees. Then some rounds find nobody: every slot of taken is true and bestIndex stays at -1. Those rounds must print nothing, so the winner handling sits inside an if (bestIndex != -1) check.
Step by step
- Create
takenwithnew bool[names.Length]. C# starts everyboolslot of a new array asfalse, which is right: nobody has won yet. - Write the outer loop with
rankcounting from 0 whilerank < k. The counter only counts the rounds, once per winner, and is not printed. - Create
bestIndexat -1 inside the round. Declaring it inside the outer loop means each round starts again with nobody chosen. - Write the inner loop over
ifrom 0 tonames.Length. Every round looks at every employee again. - Test each candidate. The employee must not have won (
!taken[i]) and either nobody is chosen yet (bestIndex == -1) or the productivity is strictly greater than that ofbestIndex. When it holds, setbestIndex = i. - Check that someone was found. After the inner loop, test
bestIndex != -1. This guards the next steps whenkis too large. - Mark the winner with
taken[bestIndex] = true;, immediately, since the next round reads it. - Compute the bonus as
salaries[bestIndex] * 15 / 100. Multiplying first and dividing second, with integers, rounds down. - Print the winner as the name at
bestIndex, then": $"and the bonus. The+operator turns the number into text when it joins it to a string.
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 k = 3;
bool[] taken = new bool[names.Length];
for (int rank = 0; rank < k; rank++) {
int bestIndex = -1;
for (int i = 0; i < names.Length; i++) {
if (!taken[i] && (bestIndex == -1 || productivities[i] > productivities[bestIndex])) {
bestIndex = i;
}
}
if (bestIndex != -1) {
taken[bestIndex] = true;
int bonus = salaries[bestIndex] * 15 / 100;
Console.WriteLine(names[bestIndex] + ": $" + bonus);
}
}
Details worth knowing:
- Short-circuit operators.
&&and||stop as soon as the answer is known. InbestIndex == -1 || productivities[i] > productivities[bestIndex], whenbestIndexis -1 the right side is never evaluated. That matters:productivities[-1]would throw anIndexOutOfRangeExceptionwith the message "Index was outside the bounds of the array." - Parentheses in the condition.
&&binds tighter than||. The parentheses make!taken[i]apply to the whole test, so a taken employee can never be chosen, whatever the productivity. - Arrays are fixed-size. The
takenarray has exactly one slot per employee and cannot change length. It is indexed with the sameias the other three arrays. - The dollar sign is plain text. In
": $"the$is just a character. It has a special meaning only in front of the opening quote, as in$"...", which starts an interpolated string. intoverflow.intarithmetic wraps silently at about 2.1 billion. The productsalaries[bestIndex] * 15overflows for salaries above 143165576. A salary of 150000000 gives the lineX: $-20449672for an employee called X. The lesson's salaries are far below that limit.
Edge cases and common mistakes
- Different values of
k. For the starting data,k = 1prints only Alice, andk = 2prints Alice and Mary. Withk = 5every employee wins once, in the order Alice, Mary, Carlos, Bob and John, whose bonuses are 37500, 50250, 27000, 31500 and 20250. - k larger than the staff. With
k = 7the output is the same five lines as for 5. The two extra rounds find nobody and print nothing, thanks to the-1check. Without it,taken[-1]would throw. k = 0. The outer loop never runs and nothing is printed.- Ties. With productivities 100, 100 and 90 for employees A, B and C, the order is A, B, C. A tie keeps the earlier employee because the comparison is strictly greater. Using
>=would make the later employee replace the earlier one. - Forgetting to mark the winner. Without
taken[bestIndex] = true, every round would pick the same employee, and the same line would printktimes. - Declaring
bestIndexoutside the outer loop. It would keep the previous winner's index into the next round, which then compares everyone against that winner instead of starting with nobody chosen, and the output would be wrong.
Testing
- The starting data with
k = 3prints Alice Chen $37500, Mary Jane $50250 and Carlos Diaz $27000. - With
k = 1onlyAlice Chen: $37500prints. - With
k = 7five lines print, ending withJohn Doe: $20250. - Employees A, B and C with productivities 100, 100 and 90, salaries 1000, 2000 and 3000 and
k = 3printA: $150,B: $300andC: $450.
