Start learning free

LESSON 85 OF 409 · FREE CODING INTERVIEW QUESTIONS

Minimum of Three Numbers in C++ Without min()

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

    C++ · FREE CODING INTERVIEW QUESTIONS

    Minimum Value Of Three in C++, explained

    Problem overview

    The method minValue(int A, int B, int C) takes three integers and returns the smallest. The statement forbids built-in helpers such as min, so the comparison has to be written by hand with if.

    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

    There are two natural ways to do this. The first checks each candidate against the others: A is the minimum if A <= B && A <= C; if not, check B the same way; if neither, it must be C. That works, but each condition names every other variable, so with four or five numbers it becomes long and easy to get wrong.

    The lesson uses the second way, a running minimum. Start by assuming A is the smallest, then look at the other values one at a time. When one is smaller than the current minimum, it replaces it. After the last value has been checked, the variable holds the answer. Adding a fourth number would need only one more if, which is why this form scales.

    For comparison, here is the first way written out. It is also correct, and it returns on the first condition that holds:

    if (A <= B && A <= C) {
        return A;
    }
    if (B <= A && B <= C) {
        return B;
    }
    return C;
    

    The conditions use <= so that a tie still counts as the minimum. With a strict <, the input (5, 5, 5) would fail both tests and be rescued only by the final return C. The running minimum has no such subtlety, because it never has to decide ties: an equal value simply changes nothing.

    Step by step

    1. Declare minVal as an int and set it to A. Until a smaller value shows up, A is the best answer so far.
    2. Write an if comparing B with minVal: B < minVal.
    3. Inside it, set minVal = B. Only a strictly smaller value replaces the minimum. If B equals minVal, nothing changes, and nothing needs to.
    4. Write a second, separate if for C. It checks C < minVal and, if true, sets minVal = C. It must be a separate if, not an else, so C is compared whether or not B replaced the minimum.
    5. Return minVal. Every value has been compared, so it is the smallest of the three.

    The solution in C++

    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;
        }
    };
    

    Walk through minValue(7, 4, 9). minVal starts at 7. Since 4 < 7, it becomes 4. Since 9 < 4 is false, it stays 4, and the method returns 4.

    The same code on (9, 8, 7) shows why the second check is separate: minVal goes 9, then 8 after B, then 7 after C, because each comparison uses the latest minimum.

    If you may use the standard library elsewhere, std::min({A, B, C}) from <algorithm> does the same in one line. This lesson asks for the manual version to practice conditions.

    The same idea extends to any number of values. For a vector<int> named v that is not empty, start with the first element and loop over the rest:

    int minVal = v[0];
    for (size_t i = 1; i < v.size(); i++) {
        if (v[i] < minVal) {
            minVal = v[i];
        }
    }
    

    This is the pattern behind finding a smallest element in every later chapter. Starting from the first element, not from 0 or a made-up large number, keeps it correct for any input, as long as there is at least one element.

    Each if can also be written with the conditional operator, minVal = (B < minVal) ? B : minVal;. It means the same thing: pick B when it is smaller, otherwise keep what is there. Beginners usually find the plain if easier to read and to step through, and behaves the same. Whichever you choose, say the logic out loud as "if this value is smaller than the best so far, it becomes the best so far". If that sentence matches your code, the code is right.

    Complexity

    Time: O(1). There are always two comparisons, whatever the values are.

    Space: O(1). The method uses one extra int.

    Edge cases and common mistakes

    • Equal values. (5, 5, 5) returns 5. Neither comparison is true, so minVal stays at A.
    • Negative numbers. (-1, -5, -3) returns -5. The comparisons treat negatives correctly: -5 < -1.
    • Extreme values. (INT_MIN, 0, INT_MAX) returns INT_MIN. The method only compares, with no subtraction or addition, so there is no overflow.
    • Using else if for the second check. If B is smaller than A and the check for C is skipped, the answer is wrong for (9, 8, 7), where C is the smallest.
    • Using = instead of == or <. if (B = minVal) assigns instead of comparing. It compiles, and the condition is true whenever the value is not 0.
    • Starting minVal at 0. If all three numbers are positive, 0 would stay the minimum, which is wrong. Start from one of the inputs.

    Testing

    • (7, 4, 9) gives 4.
    • (3, 8, 3) gives 3.
    • (9, 8, 7) gives 7.
    • (-1, -5, -3) gives -5.
    • (5, 5, 5) gives 5.
    Keep going with C++About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free