Minimum Value of Three in C++


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

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:

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:

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:

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: