Start learning free

LESSON 239 OF 671 · HELPER FUNCTIONS

Max of Three in Java: Splitting the Work with a Helper Method

Practice Read the full explanation

Given three integers a, b and c, return the largest of them.

This time the work is split between two functions. The first one is a helper: it gets two numbers and returns the larger one. The second one, the function that is graded, finds the largest of the three numbers by calling the helper, so it never compares numbers itself.

Don't use a built-in max function: the helper does the comparing. The numbers can be negative, and some of them can be equal.

 

Example 1:

Input: a = 3, b = 7, c = 5
Output: 7
Explanation: The larger of 3 and 7 is 7, and the larger of 7 and 5 is still 7.

Example 2:

Input: a = -3, b = -8, c = -1
Output: -1
Explanation: All three numbers are negative. -1 is the closest to zero, so it is the largest.

Example 3:

Input: a = 4, b = 4, c = 2
Output: 4
Explanation: a and b tie for the largest. Either one is the answer, because the value is the same: 4.

 

Constraints:

  • a, b and c are integers between -1000000 and 1000000.
  • Two or all three of them can be equal.
STEP 1 OF 3
    Ctrl + Enter to check

    JAVA · HELPER FUNCTIONS

    Max Of Three Helper in Java, explained

    Problem overview

    You write two methods in the same class. maxOfTwo(int a, int b) is a helper: it returns the larger of two numbers. maxOfThree(int a, int b, int c) returns the largest of three numbers, and it must get its answer by calling the helper instead of comparing numbers itself. Built-in max methods are off limits.

    With a = 3, b = 7, c = 5 the answer is 7. With a = -3, b = -8, c = -1 it is -1, because -1 is closest to zero. With a = 4, b = 4, c = 2 it is 4: when two numbers tie for the largest, either one is the answer because the value is the same.

    Approach

    Finding the larger of two numbers takes one comparison. Finding the largest of three can reuse that: take the larger of a and b, then take the larger of that result and c. The winner of the first round plays the third number in the second round.

    That is the whole idea of a helper method. The comparison logic is written once, in maxOfTwo, and maxOfThree only combines calls. If you later needed the largest of four numbers, you would add one more call, not another pile of if statements.

    Step by step

    1. Start inside maxOfTwo. If a > b, return a. A return ends the method on the spot, so when a is larger nothing after it runs.
    2. Return b below the if. The code only reaches this line when a is not greater than b. Then b is larger, or the two are equal and either is right. No else is needed, because the earlier return already handled the other case.
    3. Write maxOfThree as one nested call. A method calls another method of the same class by its name. maxOfTwo(a, b) gives the larger of a and b, and passing that result as the first argument of another maxOfTwo call, with c as the second, gives the largest of all three. The inner call runs first.

    Tracing the first example

    For a = 3, b = 7, c = 5, Java first evaluates maxOfTwo(3, 7), which is 7. The outer call then becomes maxOfTwo(7, 5), which is also 7. The method returns 7.

    The solution in Java

    class Solution {
        public int maxOfTwo(int a, int b) {
            if (a > b) {
                return a;
            }
            return b;
        }
    
        public int maxOfThree(int a, int b, int c) {
            return maxOfTwo(maxOfTwo(a, b), c);
        }
    }
    

    Details worth knowing:

    • Parameter names are local. Both methods have parameters called a and b, and they don't interfere. Inside maxOfTwo, a and b are copies of the values the caller passed in, whatever the caller named them. The call maxOfTwo(maxOfTwo(a, b), c) hands the helper two values, the inner result and c.
    • Primitive arguments are copied. Java passes an int by value, so the helper can't change the caller's variables.
    • The return type must match the return. Both methods are declared int, and every path through maxOfTwo ends in a return of an int. If you wrote only the if with its return a;, the compiler would reject the method, because a path would reach the closing brace without returning a value.
    • No static here. The methods belong to a Solution object, so you call them on an instance, such as new Solution().maxOfThree(3, 7, 5). Inside the class, they call each other by name alone.

    Edge cases and common mistakes

    • Negative numbers. Starting from a variable set to 0 and updating it would give 0 for -3, -8, -1. The helper compares only the given numbers, so negatives work with no special case.
    • Ties. With a = 4, b = 4, a > b is false, so the helper returns b, which is 4. Using >= would give the same value.
    • Comparing inside maxOfThree. The lesson asks the helper to do all the comparing. Adding an if to maxOfThree defeats the purpose of splitting the work.
    • Forgetting to use the result. Writing maxOfTwo(a, b); on its own line computes a value and throws it away. The result has to be returned, stored, or passed on, as the nested call does.

    Testing

    Calling maxOfThree gives these results:

    • maxOfThree(3, 7, 5) returns 7.
    • maxOfThree(-3, -8, -1) returns -1.
    • maxOfThree(4, 4, 2) returns 4.

    Calling the helper on its own, maxOfTwo(3, 7) and maxOfTwo(7, 5) both return 7, which are the two calls in the trace above.

    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free