Problem overview
The class has two methods. 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, and it must get there by calling the helper rather than comparing numbers itself. Built-in max methods are off limits, so all comparing happens in one place.
For 3, 7 and 5 the answer is 7. For -3, -8 and -1 it is -1, the negative number closest to zero. For 4, 4 and 2 it is 4: when two numbers tie, either one is the answer because the value is the same.
Approach: build the answer from a smaller one
Finding the larger of two numbers is easy. Finding the largest of three reduces to it: the largest of a, b and c is the larger of (the larger of a and b) and c. If the winner of the first pair is beaten by c, c is the maximum. Otherwise the winner of the first pair is. Either way one more call to the helper settles it.
Splitting the work this way means each method does one small job. MaxOfTwo knows how to compare. MaxOfThree only knows how to chain two comparisons.
Step by step
- In
MaxOfTwo, returnawhena > b.returnends the method on the spot, so whenais larger nothing after it runs. - Return
bbelow theif. The method only reaches this line whenais not greater thanb. Thenbis larger, or they are equal andbis just as right. Noelseis needed, because the firstreturnalready left the method. - In
MaxOfThree, nest one call inside another. A method calls another method of the same class by its name.MaxOfTwo(a, b)runs first and gives the larger of the pair. That value becomes the first argument of the outer call, which compares it withc. There is noifinMaxOfThree.
The solution in C#
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);
}
}
Things worth knowing:
- Each method has its own variables.
MaxOfTwonames its parametersaandb, and so doesMaxOfThree, but they are separate. WhenMaxOfThreecallsMaxOfTwo(MaxOfTwo(a, b), c), the outer call'sareceives the inner result and itsbreceivesc. - Arguments are evaluated before the call. The inner
MaxOfTwo(a, b)finishes first, and only then does the outer call start. - Ties are fine. With
a == bthe conditiona > bis false, so the helper returnsb, which equalsa.
Edge cases and common mistakes
- The maximum in any position.
MaxOfThree(1, 2, 9)andMaxOfThree(9, 2, 1)both return 9, because the chain checks every number once. - Negative numbers.
MaxOfThree(-3, -8, -1)returns -1. Starting a "best so far" at 0 would wrongly return 0 here, which is why the helper compares the actual inputs. - Writing the comparison twice. Putting an
ifinMaxOfThreeworks but defeats the point of the helper: if the rule ever changes, you would have to change it in two places. - Forgetting
return. A C# method with anintreturn type must return a value on every path, or the code doesn't compile.
Testing
These calls give these results:
MaxOfThree(3, 7, 5)returns 7.MaxOfThree(-3, -8, -1)returns -1.MaxOfThree(4, 4, 2)returns 4.MaxOfThree(1, 2, 9)returns 9.
