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
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:
min
are allowed.Example:
Input: A = 7, B = 4, C = 9 Output: 4
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.
To solve this problem, we can use simple conditional statements to compare the three numbers. Here are the steps:
This approach ensures that we check all possible scenarios to find the smallest number.
Here is a step-by-step breakdown of the algorithm:
#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;
}
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.
Consider the following edge cases:
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.
When approaching such problems:
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.
For further reading and practice, consider the following resources: