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:

  1. First, compare A with B and C. If A is less than or equal to both, return A.
  2. If the first 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 as 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:

  1. Initialize a variable minVal with the value of A.
  2. Compare B with minVal. If B is smaller, update minVal to B.
  3. Compare C with minVal. If C is smaller, update minVal to C.
  4. 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:

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:

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:

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: