Start learning free

LESSON 85 OF 410 · FREE CODING INTERVIEW QUESTIONS

Minimum Value of Three in Java: Compare and Keep the Smallest

Practice Read the full explanation

Given 3 integers A, B and C, return the smallest number.

You are not allowed to use built-in functions such as min

Example 1:

Input: A = 7, B = 4, C = 9
Output: 4

Example 2:

Input: A = 3, B = 8, C = 3
Output: 3


There are three candidates to the solution: A, B and C. We should try out each of them until we find the correct one. How can we check if A is the solution? What should happen for A to be the minimum value?

A is the minimum value if A <= B and A <= C. We can write an if statement checking these conditions and if they hold, return A:
if A is at most B and A is at most C: return A
What if the conditions don't hold and A is not the solution? What should we do next?

We should test B. Similarly, B is the minimum value if B <= A and B <= C. We can write an if statement checking these conditions and if they hold, return B:
if A is at most B and A is at most C: return A
if B is at most A and B is at most C: return B
What if not even B is the solution?

Then, it's clear that C is the solution, so we should return C. We don't even need to check the conditions anymore:
if A is at most B and A is at most C: return A
if B is at most A and B is at most C: return B
otherwise: return C


While the solution above is very good, we propose another one which is more scalable (i.e. works better for a larger number of values). It involves going throught the numbers and using a variable minVal to store the minimum one found so far:
minVal = A
if B is less than minVal: minVal = B
if C is less than minVal: minVal = C
return minVal
Please watch the video solution for a better understanding of this solution.

STEP 1 OF 5
    Ctrl + Enter to check

    JAVA · FREE CODING INTERVIEW QUESTIONS

    Minimum Value Of Three in Java, explained

    Problem overview

    The method minValue receives three integers A, B and C and returns the smallest one. The statement forbids built-in helpers such as min, so the comparison has to be written by hand.

    For A = 7, B = 4, C = 9 the answer is 4. For A = 3, B = 8, C = 3 the answer is 3: when the smallest value appears twice, returning either copy gives the same number.

    Approach: keep the smallest value seen so far

    One way is to test each number as a candidate: A is the minimum if A <= B and A <= C, B is the minimum if B <= A and B <= C, and if neither holds, it must be C. That works, but it writes out every pairwise comparison, and the number of conditions grows quickly with more values.

    The lesson uses a scalable idea instead. Keep a variable minVal that holds the smallest number you have looked at so far. Start with A, since after looking at one number it is also the smallest. Then look at each remaining number once: if it is smaller than minVal, replace minVal. When all numbers have been seen, minVal is the answer. For three values it takes two comparisons, and for a million values it would take 999,999: one per extra value, always.

    Here is A = 7, B = 4, C = 9. Start with minVal = 7. Is 4 less than 7? Yes, so minVal becomes 4. Is 9 less than 4? No, so it stays 4. The method returns 4.

    Step by step

    1. Start with A. Declare an int named minVal and initialize it with A. Until something smaller appears, A is the answer.
    2. Compare B. An if checks whether B is less than minVal.
    3. Update inside the if. Assign B to minVal. Only a strictly smaller value replaces the current minimum, so equal values change nothing, and that is harmless.
    4. Compare C with a second, separate if. It must not be an else if. C has to be compared with the current minimum whether or not B replaced A. With else if, the input 7, 4, 2 would stop after B and return 4.
    5. Return minVal. After both checks it holds the smallest of the three.

    The solution in Java

    class Solution {
        public int minValue(int A, int B, int C) {
            int minVal = A;
            if (B < minVal) {
                minVal = B;
            }
            if (C < minVal) {
                minVal = C;
            }
            return minVal;
        }
    }
    

    The Java details worth knowing:

    • The variable is reassigned, not redeclared. Inside the if blocks, minVal = B; has no type in front. Writing int minVal = B; there does not compile, because minVal is already declared in the enclosing scope and Java does not allow a second local variable with the same name.
    • Parameter names. A, B and C are valid Java names, though by convention parameters start with a lowercase letter. The names here match the lesson.
    • The library way. Outside this exercise, the usual one-liner is Math.min(a, Math.min(b, c)), because Math.min takes exactly two arguments. The exercise asks you to do by hand what that call does.
    • A conditional expression can shorten each step: minVal = (B < minVal) ? B : minVal;. The if form is easier to read and extend.

    Complexity

    Time: O(1) for three values. The method makes exactly two comparisons whatever the numbers are. If the same idea were applied to n values in an array, it would be O(n): one comparison per element.

    Space: O(1). The method uses one extra variable, minVal.

    Edge cases and common mistakes

    • Equal values. 3, 8, 3 returns 3, and 5, 5, 5 returns 5. Using < or <= gives the same number, because swapping a value for an equal one changes nothing.
    • Negative numbers. -1, -5, -3 returns -5. The comparison works on the numbers' real values, so -5 is smaller than -1.
    • Extreme values. Integer.MIN_VALUE and Integer.MAX_VALUE work, because comparing two int values never overflows. Computing a difference, such as A - B < 0, can overflow and give a wrong answer, so compare directly.
    • Using else if for the second check, as described in step 4.
    • Starting minVal at 0. For all-positive inputs the result would be 0, which is not one of the numbers. Always start from one of the actual values.
    • Returning the position instead of the value. The method returns the number itself, not which parameter held it.

    Testing

    • 7, 4, 9 gives 4.
    • 3, 8, 3 gives 3.
    • 1, 2, 3 gives 1, and 3, 2, 1 gives 1.
    • -1, -5, -3 gives -5.
    • 5, 5, 5 gives 5.
    • Integer.MIN_VALUE, 0, Integer.MAX_VALUE gives -2147483648.
    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free