Start learning free

LESSON 183 OF 671 · LOOPING OVER ARRAY INDICES

Employee Of The Year V4 in Java: Finding the Best with a for Loop

Practice Read the full explanation

A whole team is competing. Use a for loop over the arrays to find the most productive employee, then print their 15% bonus.

You start with:

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};

Build:

Alice Chen
Bonus: $37500

Rules:

  • Index i in all three arrays refers to the same employee.
  • The winner is whichever employee has the highest productivity. If two employees share the highest productivity, the one listed first wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Start with int bestIndex = 0 and loop over the other indices with for (int i = 1; i < names.length; i++). Inside the loop, update the best index only when productivities[i] > productivities[bestIndex]: a strict comparison keeps the first employee on a tie.

Dividing two whole numbers (int) drops the fraction, so salaries[bestIndex] * 15 / 100 is already rounded down: int bonus = salaries[bestIndex] * 15 / 100;

+ turns the int into text: System.out.println("Bonus: $" + bonus);

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);

STEP 1 OF 6
    Ctrl + Enter to check

    JAVA · LOOPING OVER ARRAY INDICES

    Employee Of The Year V4 in Java, explained

    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

    1. Create bestIndex and set it to 0. Until someone beats employee 0, they are the best known.
    2. Write a for loop with i starting at 1 and running while i < names.length. It starts at 1 because employee 0 is already the champion, and comparing them with themselves would change nothing.
    3. Inside the loop, compare productivities[i] with productivities[bestIndex] using >. When the challenger is higher, set bestIndex = i.
    4. After the loop, compute bonus from salaries[bestIndex], multiplied by 15 and divided by 100.
    5. Print names[bestIndex] on its own line.
    6. 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).

    iproductivities[i]bestIndex after the check
    11331
    2971
    31503
    41203

    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.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free