Start learning free

LESSON 242 OF 291 · HELPER FUNCTIONS

Max of Three with a Helper in Kotlin

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

    KOTLIN · HELPER FUNCTIONS

    Max Of Three Helper in Kotlin, explained

    Problem overview

    Given three integers a, b and c, return the largest. The lesson splits the work between two functions in the same class. The helper maxOfTwo takes two numbers and returns the larger one. The function that is checked, maxOfThree, must find the largest of the three by calling the helper. It never compares numbers itself, and 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: when two numbers tie, either one is the answer because the value is the same.

    Approach

    Finding the larger of two numbers is a single comparison. Finding the largest of three is two such comparisons chained together: the winner of a against b then meets c. Writing the pairwise comparison once, in a helper, means maxOfThree only has to arrange the calls.

    Step by step

    The helper. Inside maxOfTwo, if a > b the function returns a straight away. A return ends the function on the spot, so nothing below it runs in that case.

    The next line returns b. The function only reaches it when a is not greater than b, which means b is larger or the two are equal, and in both cases b is a correct answer. That is why no else is needed.

    The main function. maxOfThree returns maxOfTwo(maxOfTwo(a, b), c). Inside a class, one function calls another by its name, so there is no object or prefix to write. The inner call runs first and gives the larger of a and b. That result becomes the first argument of the outer call, which compares it with c.

    The solution in Kotlin

    class Solution {
        fun maxOfTwo(a: Int, b: Int): Int {
            if (a > b) {
                return a
            }
            return b
        }
    
        fun maxOfThree(a: Int, b: Int, c: Int): Int {
            return maxOfTwo(maxOfTwo(a, b), c)
        }
    }
    

    Tracing maxOfThree(3, 7, 5): the inner call maxOfTwo(3, 7) returns 7, then maxOfTwo(7, 5) returns 7. For maxOfThree(1, 2, 9) the inner call returns 2, and maxOfTwo(2, 9) returns 9, so the largest value can be in any of the three positions.

    Some Kotlin details:

    • Every function declares its parameter types and its return type, written after the colon: : Int.
    • A function that has a block body and declares a return type must return on every path. Both return statements in the helper make sure of that.
    • The parameters of the two functions share the names a and b, but each function has its own variables. The call maxOfTwo(maxOfTwo(a, b), c) passes values, so the helper's a and b are separate from maxOfThree's.

    Edge cases and common mistakes

    • Negative numbers. A common mistake is to start from 0 as "the best so far". With -3, -8, -1 that would return 0, which isn't one of the inputs. The pairwise comparison never invents a value.
    • Equal numbers. With a == b the condition a > b is false, so the helper returns b, which has the same value as a. maxOfTwo(5, 5) returns 5.
    • Comparing inside maxOfThree. Adding if checks there defeats the lesson's idea. The helper should do all the comparing.
    • Wrong nesting. maxOfTwo(a, b, c) doesn't compile, because the helper takes exactly two arguments. The calls have to be nested.

    Testing

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