Start learning free

LESSON 83 OF 343 · FREE CODING INTERVIEW QUESTIONS

Minimum Value of Three in C++

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 <= B and A <= 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 <= B and A <= C:
	return A
if B <= A and B <= 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 <= B and A <= C:
	return A
if B <= A and B <= C:
	return B
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 < minVal:
	minVal = B
if C < 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 Definition

    The task is to find the smallest of three given integers A, B, and C without using built-in functions like min.

    Input: Three integers A, B, and C.

    Output: The smallest integer among the three.

    Constraints:

    • All inputs are integers.
    • No built-in functions like min are allowed.

    Example:

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

    Understanding the Problem

    The core challenge is to determine the smallest of three numbers without using built-in functions. This problem is fundamental in understanding basic conditional statements and comparisons in programming.

    Common applications include scenarios where built-in functions are restricted or unavailable, and understanding the logic behind such comparisons is crucial.

    Potential pitfalls include incorrect comparisons or overlooking edge cases where two or more numbers are equal.

    Approach

    To solve this problem, we can use simple conditional statements to compare the three numbers. Here are the steps:

    1. Compare A with B and C. If A is less than or equal to both, return A.
    2. If the above condition is not met, compare B with A and C. If B is less than or equal to both, return B.
    3. If neither of the above conditions is met, return C.

    This approach ensures that we check all possible scenarios to find the smallest number.

    Algorithm

    Here is a step-by-step breakdown of the algorithm:

    1. Initialize the function with three integer parameters A, B, and C.
    2. Use an if statement to check if A is less than or equal to both B and C. If true, return A.
    3. If the above condition is false, use another if statement to check if B is less than or equal to both A and C. If true, return B.
    4. If neither of the above conditions is true, return C.

    Code Implementation

    #include <iostream>
    using namespace std;
    
    // Function to find the minimum of three numbers
    int findMinimum(int A, int B, int C) {
        // Check if A is the smallest
        if (A <= B && A <= C) {
            return A;
        }
        // Check if B is the smallest
        if (B <= A && B <= C) {
            return B;
        }
        // If neither A nor B is the smallest, return C
        return C;
    }
    
    int main() {
        int A = 7, B = 4, C = 9;
        cout << "The minimum value is: " << findMinimum(A, B, C) << endl;
        return 0;
    }
    

    Complexity Analysis

    The time complexity of this approach is O(1) because the number of comparisons is constant and does not depend on the input size.

    The space complexity is also O(1) as we are not using any additional space that grows with the input size.

    Edge Cases

    Consider the following edge cases:

    • All three numbers are equal. For example, A = 3, B = 3, C = 3. The output should be 3.
    • Two numbers are equal and the smallest. For example, A = 3, B = 3, C = 5. The output should be 3.
    • Negative numbers. For example, A = -1, B = -2, C = -3. The output should be -3.

    Testing

    To test the solution comprehensively, consider the following test cases:

    A = 7, B = 4, C = 9  -> Output: 4
    A = 3, B = 8, C = 3  -> Output: 3
    A = -1, B = -2, C = -3  -> Output: -3
    A = 0, B = 0, C = 0  -> Output: 0
    A = 5, B = 5, C = 5  -> Output: 5
    

    Testing frameworks like Google Test can be used for automated testing.

    Thinking and Problem-Solving Tips

    When approaching such problems:

    • Break down the problem into smaller parts.
    • Use simple conditional statements to compare values.
    • Consider edge cases and test your solution against them.
    • Practice similar problems to improve your problem-solving skills.

    Conclusion

    In this blog post, we discussed how to find the minimum of three integers without using built-in functions. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

    Additional Resources

    For further reading and practice, consider the following resources:

    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