QUESTION #023

Find Minimum of Three Numbers

Easy

📋 Problem Statement

Find the minimum value among three given numbers.

🎯 Comparison Logic

To find the minimum of three numbers a, b, and c:

  • Compare each number with the other two
  • The number that is smaller than both others is the minimum
  • Use logical AND (&&) to check both conditions simultaneously
Finding Minimum: Progressive Comparison
Numbers:  a = 10,  b = 5,  c = 8

Step 1: Is a < b AND a < c?
        10 < 5? NO  →  a is not minimum

Step 2: Is b < a AND b < c?
        5 < 10? YES  AND  5 < 8? YES  →  b is minimum!

Result: Minimum is 5

📥 Input Format

Accept three integers as input

📤 Output Format

Print the output as "Minimum is ___"

💡 Examples

Example 1:
Input: 10 20 30
Output: Minimum is 10
Analysis:
• a = 10, b = 20, c = 30
• Check: Is 10 < 20? YES, Is 10 < 30? YES
• 10 is smaller than both 20 and 30
Result: Minimum is 10
Example 2:
Input: 1 6 3
Output: Minimum is 1
Analysis:
• a = 1, b = 6, c = 3
• Check: Is 1 < 6? YES, Is 1 < 3? YES
• 1 is the smallest value
Result: Minimum is 1
Example 3:
Input: 50 25 75
Output: Minimum is 25
Analysis:
• a = 50, b = 25, c = 75
• Check a: 50 < 25? NO → not minimum
• Check b: 25 < 50? YES, 25 < 75? YES → minimum!
Result: Minimum is 25
Example 4 (Equal Values):
Input: 5 5 10
Output: Minimum is 5
Analysis:
• a = 5, b = 5, c = 10
• Check a: 5 < 5? NO, 5 < 10? YES → not both true
• Check b: 5 < 5? NO, 5 < 10? YES → not both true
• Else clause: c is assigned to minimum
• But wait! The logic needs adjustment for equal values
Note: When values are equal, the else clause picks the last one, but the minimum value is still correct.
Example 5 (Negative Numbers):
Input: -5 -10 -3
Output: Minimum is -10
Analysis:
• a = -5, b = -10, c = -3
• Remember: -10 is smaller than -5 and -3
• Check b: -10 < -5? YES, -10 < -3? YES → minimum!
Result: Minimum is -10

⚠️ Constraints

-10^15 <= INPUT <= 10^15

✅ Solution

#include <iostream>
#include <cmath>  // Note: min() is actually in <algorithm>, not <cmath>

using namespace std;

int main() {
    long long a, b, c, minimum;
    cin >> a >> b >> c;
    
    // Method 1: Using built-in min() function (commented out)
    // minimum = min(min(a, b), c);  // Requires <algorithm>
    
    // Method 2: Using if-else logic
    if (a < b && a < c)
        minimum = a;
    else if (b < a && b < c)
        minimum = b;
    else
        minimum = c;
    
    cout << "Minimum is " << minimum;
    
    return 0;
}

⚠️ About the Header Files

The code includes <cmath>, but the min() function is actually defined in <algorithm>. The code may still work on some compilers because <cmath> sometimes transitively includes <algorithm>, but this is compiler-dependent and not guaranteed by the C++ standard. For portable code, always include <algorithm> when using min() or max().

🔑 Key Concepts

  • Comparison Operators: Using less than (<) to compare values
  • Logical AND (&&): Both conditions must be true for a number to be minimum
  • If-Else-If Chain: Testing conditions sequentially until one is true
  • Else Clause: Handles remaining case when neither a nor b is minimum
  • Long Long Data Type: Handling very large values (up to 10^15)
  • Alternative Methods: Built-in min() function vs manual comparison

💡 Understanding the Logic Flow

Decision Tree:

Is a < b AND a < c?
├── YES → minimum = a ✓ (DONE)
└── NO → Continue...
    
    Is b < a AND b < c?
    ├── YES → minimum = b ✓ (DONE)
    └── NO → minimum = c ✓ (DONE)
    
Logic: If neither a nor b is smallest, then c must be!
                    

Why the Else Clause Works:

  • If a is not smaller than both others → it's not minimum
  • If b is not smaller than both others → it's not minimum
  • Therefore, c must be the minimum (or equal to minimum)
  • This logic works even when values are equal

🚀 Using the Built-in min() Function

C++ provides the min() function from the <algorithm> header:

#include <iostream>
#include <algorithm>  // Required for min() function

using namespace std;

int main() {
    long long a, b, c;
    cin >> a >> b >> c;
    
    // Nested min() - find min of (a,b), then compare with c
    long long minimum = min(min(a, b), c);
    
    cout << "Minimum is " << minimum;
    
    return 0;
}

How it works:

  • min(a, b) → returns the smaller of a and b
  • min(result, c) → compares that result with c
  • Final result is the minimum of all three

C++11 and later: You can also use initializer lists:

long long minimum = min({a, b, c});  // Requires C++11 or later

⚠️ Important Note:

The code includes <cmath> instead of <algorithm>, but it still works on many compilers because <cmath> sometimes implicitly includes <algorithm>. However, this is non-standard behavior! Always use <algorithm> when you need min() or max() functions for portability.

⚖️ Manual Logic vs Built-in Function

Aspect Manual If-Else Logic Built-in min() Function
Code Length Longer (7-8 lines) Shorter (1 line)
Readability Clear logic flow More concise
Performance Similar (compiler optimizes both) Similar
Learning Value High (understand comparison logic) Medium
Flexibility Easy to add custom logic Limited customization
Error Prone More chances for logic errors Less error-prone
Best For Learning, interviews, custom needs Production code, quick solutions

⚠️ Common Mistakes to Avoid

  • Incomplete Comparisons: Only checking one condition
    // ❌ WRONG: Only checks if a < b, not if a < c
    if (a < b)
        minimum = a;  // What if c is smaller than a?
  • Using OR instead of AND: Would give wrong results
    // ❌ WRONG: a must be smaller than BOTH b and c
    if (a < b || a < c)  // Using OR is incorrect!
        minimum = a;
  • Missing Else Clause: Variable might remain uninitialized
  • Wrong Operator: Using > instead of < for minimum
  • Not Handling Equal Values: Logic should work even when numbers are equal
  • Data Type Mismatch: Using int for large values instead of long long

🎯 Alternative Approaches

Approach 1: Simplified If-Else (Assuming a is minimum first)

long long minimum = a;  // Assume a is minimum

if (b < minimum)
    minimum = b;  // Update if b is smaller

if (c < minimum)
    minimum = c;  // Update if c is smaller

cout << "Minimum is " << minimum;

Approach 2: Ternary Operator (Nested)

long long minimum = (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);

cout << "Minimum is " << minimum;

Approach 3: Using Array and Loop (Scalable)

long long arr[3] = {a, b, c};
long long minimum = arr[0];

for (int i = 1; i < 3; i++) {
    if (arr[i] < minimum)
        minimum = arr[i];
}

cout << "Minimum is " << minimum;

🌍 Real-world Applications

  • E-commerce: Finding the lowest price among multiple sellers
  • Route Planning: Selecting the shortest path among alternatives
  • Resource Allocation: Choosing the server with minimum load
  • Game Development: Finding minimum distance to enemies or objects
  • Data Analysis: Identifying minimum values in datasets
  • Finance: Finding minimum stock prices for buying decisions
  • Scheduling: Selecting the earliest available time slot
  • Quality Control: Identifying minimum acceptable standards
  • Network Optimization: Finding minimum latency routes

🎓 Extension Challenges

  • Level 1: Find minimum of 4 or 5 numbers
  • Level 2: Find both minimum AND maximum in one program
  • Level 3: Find the second smallest number among three
  • Level 4: Find minimum of N numbers (user inputs N)
  • Level 5: Find minimum and its position (which input was it?)
  • Level 6: Handle case where all three numbers are equal differently
  • Bonus: Sort three numbers in ascending order

🎯 What You'll Learn

  • Understanding comparison logic and relational operators
  • Using logical AND (&&) to combine multiple conditions
  • Building if-else-if chains for multiple checks
  • Working with built-in library functions (min)
  • Choosing between manual logic and built-in functions
  • Handling edge cases (equal values, negative numbers)
  • Process of elimination in conditional logic
  • Importance of proper data types for large values

💪 Practice Tips

  • Test All Positions: Try cases where minimum is first, second, third position
  • Test Equal Values: (5, 5, 10), (5, 10, 5), (10, 5, 5), (5, 5, 5)
  • Test Negative Numbers: (-5, -10, -3), (-1, -2, -3)
  • Test Zero: (0, 5, -5), (0, 0, 0)
  • Test Large Values: Use values close to 10^15
  • Trace Through Logic: Manually walk through each condition with test data
  • Compare Methods: Implement both manual and built-in approaches