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 core challenge of this problem is to determine the smallest of three given integers without using built-in functions like Math.min
. This is a fundamental problem that helps in understanding basic conditional logic and comparison operations.
Common applications of this problem include scenarios where you need to find the minimum value in a set of numbers, which is a frequent requirement in algorithms and data processing.
Potential pitfalls include not correctly handling the comparisons or missing 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’s a step-by-step approach:
A
with B
and C
. If A
is less than or equal to both, return A
.B
with A
and C
. If B
is less than or equal to both, return B
.C
as it must be the smallest.This approach ensures that we check all possibilities and return the correct minimum value.
Here’s a step-by-step breakdown of the algorithm:
minVal
with the value of A
.B
with minVal
. If B
is smaller, update minVal
to B
.C
with minVal
. If C
is smaller, update minVal
to C
.minVal
.Here is the JavaScript code for the solution:
// Function to find the minimum of three numbers
function findMinimum(A, B, C) {
// Initialize minVal with A
let minVal = A;
// Compare B with minVal
if (B < minVal) {
minVal = B;
}
// Compare C with minVal
if (C < minVal) {
minVal = C;
}
// Return the minimum value
return minVal;
}
// Example usage:
console.log(findMinimum(7, 4, 9)); // Output: 4
console.log(findMinimum(3, 8, 3)); // Output: 3
The time complexity of this solution is O(1) because we are performing a constant number of comparisons regardless of the input size. The space complexity is also O(1) as we are using a fixed amount of extra space.
Potential edge cases include:
A = 5, B = 5, C = 5
). The function should return 5.A = 2, B = 2, C = 3
). The function should return 2.These cases are handled correctly by the algorithm as it checks all conditions systematically.
To test the solution comprehensively, consider the following test cases:
Example test cases:
console.log(findMinimum(7, 4, 9)); // Output: 4
console.log(findMinimum(3, 8, 3)); // Output: 3
console.log(findMinimum(-1, -5, -3)); // Output: -5
console.log(findMinimum(0, 0, 0)); // Output: 0
console.log(findMinimum(1, 2, 1)); // Output: 1
When approaching such problems, consider the following tips:
In this blog post, we discussed how to find the minimum of three numbers 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.
Keep practicing and exploring further to enhance your skills!
For further reading and practice problems, consider the following resources:
Our interactive tutorials and AI-assisted learning will help you master problem-solving skills and teach you the algorithms to know for coding interviews.
Start Coding for FREE