Start learning free

LESSON 238 OF 667 · HELPER FUNCTIONS

Max of Three with a Helper Function in C++

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

    C++ · HELPER FUNCTIONS

    Max Of Three Helper in C++, explained

    Problem overview

    Given three integers a, b and c, return the largest. The lesson splits the work into two member functions of Solution. The helper maxOfTwo takes two numbers and returns the larger one. The function that is checked, maxOfThree, must find the maximum by calling the helper and must not compare numbers itself. Built-in max functions are off limits.

    For 3, 7, 5 the answer is 7. For -3, -8, -1 it is -1, the negative number closest to zero. For 4, 4, 2 it is 4, and a tie causes no problem because both candidates have the same value.

    Approach: reduce three numbers to two, twice

    If you can pick the larger of two numbers, you can pick the largest of three: take the larger of the first two, then compare that winner with the third. The helper does one comparison, and maxOfThree uses it twice. The same pattern extends to any number of values.

    The benefit of a helper is that the comparison logic lives in one place. maxOfThree reduces to one line, and a bug in the comparison would be fixed once.

    Step by step

    1. In maxOfTwo, return a when a > b. return ends the function immediately, so nothing after it runs for that call.
    2. Return b below the if. Execution only reaches this line when a is not greater than b. Then b is larger, or the two are equal and either is correct. No else is needed.
    3. In maxOfThree, nest two calls. The inner call, maxOfTwo(a, b), runs first and produces the larger of a and b. That value is the first argument of the outer call, whose second argument is c.

    The solution in C++

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

    Details worth knowing:

    • Calling a sibling member function. Inside a class, maxOfTwo(a, b) works with no object or prefix, because both functions belong to the same Solution object. Inside a class definition, the member function bodies can use any member, whatever the order in which they are written.
    • Separate variables. The parameters a and b of maxOfTwo are not the a and b of maxOfThree. Each call gets its own copies of the values.
    • Evaluation order. In a nested call, the inner call finishes before the outer one starts, because its result is an argument of the outer call.
    • Passing by value. int parameters are copies, which is the right choice for small types like this.

    Edge cases and common mistakes

    • Negative numbers. maxOfThree(-3, -8, -1) returns -1. Starting from a "best so far" of 0 would wrongly give 0 here, but this solution never needs a starting value.
    • Ties. maxOfThree(4, 4, 2) returns 4. maxOfTwo(5, 5) returns 5 as well, through the final return b.
    • The largest value in any position. maxOfThree(9, 1, 2), maxOfThree(1, 2, 9) and the middle position all return 9, and maxOfThree(0, 0, 0) returns 0.
    • Comparing inside maxOfThree. Adding if statements there duplicates the helper's job and breaks the lesson's rule.

    Testing

    • maxOfThree(3, 7, 5) returns 7.
    • maxOfThree(-3, -8, -1) returns -1.
    • maxOfThree(4, 4, 2) returns 4.
    • maxOfThree(9, 1, 2) and maxOfThree(1, 2, 9) both return 9.
    • maxOfThree(0, 0, 0) returns 0.
    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free