Problem overview
The team is bigger now, and the data is stored in three parallel arrays: names, productivities and salaries. Index i in all three arrays describes the same employee. The program finds the employee with the highest productivity, then prints their name and a 15% bonus, rounded down. If several employees share the highest productivity, the one listed first wins.
With the starter data, Alice Chen (index 3) has the top productivity, 150, and her salary is 250000:
Alice Chen
Bonus: $37500
Approach: remember the best index while scanning
The earlier versions used an if chain with one branch per employee. That cannot work for "any number of employees": the code would have to change whenever the team does. A loop handles any length with the same few lines.
The idea is to keep the index of the best employee seen so far in bestIndex. Storing the index, not the productivity, is the key choice: one index gives you the name, the productivity and the salary at once, because the arrays are parallel. Start by assuming employee 0 is the best, then visit the others one by one and replace the champion whenever someone beats them.
The comparison is strictly greater (>). On a tie, the challenger does not replace the champion, so the employee who appears first stays the best.
Step by step
- Create
bestIndexand set it to 0. Until someone beats employee 0, they are the best known. - Write a
forloop withistarting at 1 and running whilei < names.length. It starts at 1 because employee 0 is already the champion, and comparing them with themselves would change nothing. - Inside the loop, compare
productivities[i]withproductivities[bestIndex]using>. When the challenger is higher, setbestIndex = i. - After the loop, compute
bonusfromsalaries[bestIndex], multiplied by 15 and divided by 100. - Print
names[bestIndex]on its own line. - Print the bonus as
"Bonus: $"followed by the number.
The solution in Java
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 bonus = salaries[bestIndex] * 15 / 100;
System.out.println(names[bestIndex]);
System.out.println("Bonus: $" + bonus);
The loop uses names.length, the number of elements, as its limit. Valid indexes run from 0 to length - 1, so the condition is i < names.length, never <=. Note that length on an array is a field, written without parentheses.
Tracing the loop
Here is what the loop does with the starter data. Before it, bestIndex is 0 (productivity 83).
i | productivities[i] | bestIndex after the check |
|---|---|---|
| 1 | 133 | 1 |
| 2 | 97 | 1 |
| 3 | 150 | 3 |
| 4 | 120 | 3 |
The loop ends with bestIndex equal to 3, so the program prints names[3], Alice Chen, and her bonus.
Complexity
The loop visits each employee once, so the time is O(n) for n employees. It uses a few extra variables, so the extra space is O(1).
Edge cases and common mistakes
Ties
With productivities {150, 133, 97, 150, 120}, John Doe (index 0) and Alice Chen (index 3) both have 150. Because the check is strict, the program prints John Doe and Bonus: $20250. Changing > to >= would let Alice replace him.
Off-by-one in the loop limit
Using i <= names.length makes the last pass read index 5 in an array of 5 elements, which throws an ArrayIndexOutOfBoundsException.
Tracking the value instead of the index
If you store the best productivity in a variable, you know the number but not who owns it, and you would need a second search to find the name and salary. Keeping the index avoids that.
An empty team
The solution assumes at least one employee. With empty arrays, productivities[bestIndex] is never reached, but salaries[bestIndex] reads index 0 of an empty array and throws ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0. This lesson's data always has employees, but a real program would check names.length first.
Key takeaways
- To find a maximum, keep the best so far and challenge it with every other element.
- Remember the index when you have parallel arrays: it gives access to every attribute of the winner.
- A strict comparison keeps the first of equal values.
