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
- In
maxOfTwo, returnawhena > b.returnends the function immediately, so nothing after it runs for that call. - Return
bbelow theif. Execution only reaches this line whenais not greater thanb. Thenbis larger, or the two are equal and either is correct. Noelseis needed. - In
maxOfThree, nest two calls. The inner call,maxOfTwo(a, b), runs first and produces the larger ofaandb. That value is the first argument of the outer call, whose second argument isc.
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 sameSolutionobject. Inside a class definition, the member function bodies can use any member, whatever the order in which they are written. - Separate variables. The parameters
aandbofmaxOfTwoare not theaandbofmaxOfThree. 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.
intparameters 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 finalreturn b. - The largest value in any position.
maxOfThree(9, 1, 2),maxOfThree(1, 2, 9)and the middle position all return 9, andmaxOfThree(0, 0, 0)returns 0. - Comparing inside
maxOfThree. Addingifstatements 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)andmaxOfThree(1, 2, 9)both return 9.maxOfThree(0, 0, 0)returns 0.
