Minimum Value of Three in Java


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. This is a fundamental problem that helps 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 missing conditions, which can lead to incorrect results.

Approach

To solve this problem, we can use simple conditional statements to compare the three integers. 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 as it is the smallest.

This approach ensures that we check all possible conditions 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

public class MinimumValueOfThree {
    // Function to find the minimum of three integers
    public static 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;
    }

    // Main method to test the function
    public static void main(String[] args) {
        // Test cases
        System.out.println(findMinimum(7, 4, 9)); // Output: 4
        System.out.println(findMinimum(3, 8, 3)); // Output: 3
    }
}

Complexity Analysis

The time complexity of this approach 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 not using any additional space that grows with the input size.

Edge Cases

Potential edge cases include:

These cases are handled by the conditional checks in the algorithm.

Testing

To test the solution comprehensively, consider the following test cases:

Use a variety of test cases to ensure the function works correctly in all scenarios.

Thinking and Problem-Solving Tips

When approaching such problems, consider breaking down the problem into smaller parts and solving each part step-by-step. Practice solving similar problems to improve your problem-solving skills. Understanding the logic behind comparisons and conditional statements is crucial.

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 important for developing strong problem-solving skills in programming.

Additional Resources

For further reading and practice problems, consider the following resources: