Minimum Value of Three (Time Complexity: O(1), Language: JavaScript)
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
Understanding the Problem
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.
Approach
To solve this problem, we can use simple conditional statements to compare the three numbers. Here’s a step-by-step approach:
- First, compare
AwithBandC. IfAis less than or equal to both, returnA. - If the first condition is not met, compare
BwithAandC. IfBis less than or equal to both, returnB. - If neither of the above conditions is met, return
Cas it must be the smallest.
This approach ensures that we check all possibilities and return the correct minimum value.
Algorithm
Here’s a step-by-step breakdown of the algorithm:
- Initialize a variable
minValwith the value ofA. - Compare
BwithminVal. IfBis smaller, updateminValtoB. - Compare
CwithminVal. IfCis smaller, updateminValtoC. - Return
minVal.
Code Implementation
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
Complexity Analysis
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.
Edge Cases
Potential edge cases include:
- All three numbers are the same (e.g.,
A = 5, B = 5, C = 5). The function should return 5. - Two numbers are the same and smaller than the third (e.g.,
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.
Testing
To test the solution comprehensively, consider the following test cases:
- Simple cases with distinct numbers.
- Cases where two or more numbers are the same.
- Cases with negative numbers.
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
Thinking and Problem-Solving Tips
When approaching such problems, consider the following tips:
- Break down the problem into smaller, manageable parts.
- Think about all possible scenarios and edge cases.
- Write pseudo-code before actual implementation to clarify your thought process.
- Practice similar problems to improve your problem-solving skills.
Conclusion
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!
Additional Resources
For further reading and practice problems, consider the following resources: