5. Find Minimum of Two Numbers

Easy ⏱️ 8 min

Problem Statement

Given 2 numbers, find the minimum of these 2 numbers

Input Format:
Accept two integer values as inputs.
Output Format:
Print the output as "Minimum of INPUT1 and INPUT2 is ____".
Constraints:
-10^15 <= INPUT <= 10^15
Sample Input 1:
10 200
Sample Output 1:
Minimum of 10 and 200 is 10
Sample Input 2:
10 20
Sample Output 2:
Minimum of 10 and 20 is 10
🧱 Finding Minimum: Logic vs Library Functions
🧠 The Logic-Building Approach (Our Solution)

Using if-else to find minimum:

if (a < b)
  min = a;
else
  min = b;

How it works:

  1. Compare two numbers: a < b
  2. If TRUE → a is smaller, store it in min
  3. If FALSE → b is smaller or equal, store it in min

Why learn this approach?

  • Builds fundamental understanding of comparison logic
  • Essential for interviews and problem-solving
  • Forms the foundation for more complex algorithms
  • You understand exactly what's happening

💡 Edge case: When both numbers are equal (a == b), either can be the minimum. Our else handles this by choosing b.

📚 The <cmath> Library Approach (Alternative)

Using the built-in min() function:

#include <cmath>
cout << "Minimum is " << min(a, b);

How it works:

The min(a, b) function takes two values and returns the smaller one.

min(10, 200) → Returns 10

min(50, 50) → Returns 50

Advantages:

  • Shorter, more concise code
  • Less chance of making mistakes
  • Optimized by the compiler
  • Industry-standard approach

Disadvantage:

  • You don't build the comparison logic yourself
  • Less understanding of how it works internally

Alternative solution using min():

// Commented line in our code:
cout << "Minimum of " << a << " and " << b << " is " << min(a,b);

🔬 Introduction to <cmath> Library

The <cmath> library provides mathematical functions and constants.

To use it:

#include <cmath>

Common functions in <cmath>:

  • min(a, b) → Returns minimum of two values
  • max(a, b) → Returns maximum of two values
  • abs(x) → Returns absolute value (removes negative sign)
  • pow(x, y) → Returns x raised to power y (x^y)
  • sqrt(x) → Returns square root of x
  • ceil(x) → Rounds up to nearest integer
  • floor(x) → Rounds down to nearest integer
  • round(x) → Rounds to nearest integer

Examples:

  • min(5, 10) → 5
  • max(5, 10) → 10
  • abs(-25) → 25
  • pow(2, 3) → 8 (2³)
  • sqrt(16) → 4
  • ceil(4.2) → 5
  • floor(4.8) → 4
  • round(4.5) → 5
🆚 When to Use Logic vs Library Functions?

Use Logic-Building Approach when:

  • 🎓 Learning fundamentals and building problem-solving skills
  • 📝 In interviews where you need to show your thinking process
  • 🧩 Solving complex problems where no built-in function exists
  • 🔍 You need to understand exactly how something works

Use Library Functions when:

  • ⚡ Building production code (faster, more reliable)
  • 🏗️ Working on real-world projects with tight deadlines
  • ✅ The logic is straightforward and well-tested
  • 📦 You want cleaner, more maintainable code

💡 Best practice: Learn the logic first (like we're doing!), then use library functions for efficiency. This way you understand what's happening under the hood.

🔍 Step-by-Step Code Execution

Example with a=10, b=200:

  1. Declare variables: long long a, b, min;
  2. Read input: a = 10, b = 200
  3. Check condition: if (10 < 200) → TRUE
  4. Execute: min = a;min = 10
  5. Print: "Minimum of 10 and 200 is 10"

Example with a=100, b=20:

  1. Declare variables: long long a, b, min;
  2. Read input: a = 100, b = 20
  3. Check condition: if (100 < 20) → FALSE
  4. Go to else: min = b;min = 20
  5. Print: "Minimum of 100 and 20 is 20"

Example with a=50, b=50:

  1. Declare variables: long long a, b, min;
  2. Read input: a = 50, b = 50
  3. Check condition: if (50 < 50) → FALSE (equal, not less)
  4. Go to else: min = b;min = 50
  5. Print: "Minimum of 50 and 50 is 50"
💭 Why is <cmath> Included but Not Used?

You might notice we include <cmath> but don't use it in the solution.

Reason:

This demonstrates that there are two approaches to solving the problem:

  1. Logic-based approach (what we used) - Better for learning
  2. Library function approach (commented out) - More practical

The commented line shows the alternative:

// cout << "Minimum of " << a << " and " << b << " is " << min(a,b);

✅ Including the library prepares you to use min(a, b) if needed, but we choose the manual approach to build stronger problem-solving skills.

💡 In competitive programming or interviews, showing you can build the logic manually is often more impressive than just using a library function!

💡 Tip: Master the logic first, then leverage library functions for efficiency. Best of both worlds!

Solution

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    long long a, b, min;
    
    cin >> a >> b;
    
    // cout << "Minimum of " << a << " and " << b << " is " << min(a,b);
    
    if (a < b)
        min = a;
    else
        min = b;
    
    cout << "Minimum of " << a << " and " << b << " is " << min;
}