6. Find Maximum of Two Numbers

Easy ⏱️ 8 min

Problem Statement

Given 2 numbers, find the maximum of 2 numbers

Input Format:
Accept two integer values as a input
Output Format:
Print the output as "Maximum of INPUT1 and INPUT2 is ____".
Constraints:
-10^15 <= INPUT <= 10^15
Sample Input 1:
98 123
Sample Output 1:
Maximum of 98 and 123 is 123
Sample Input 2:
0 68
Sample Output 2:
Maximum of 0 and 68 is 68
🧱 Finding Maximum: Logic vs max() Function
📈 The Greater Than Operator (>)

The > operator checks if the left value is strictly greater than the right value.

Syntax: value1 > value2

Returns: true if value1 is greater than value2, otherwise false

Examples:

  • 123 > 98 → TRUE (123 is greater than 98)
  • 98 > 123 → FALSE (98 is not greater than 123)
  • 50 > 50 → FALSE (50 is NOT greater than 50, they're equal)
  • 0 > -5 → TRUE (0 is greater than -5)

Key point: The value must be strictly larger, not equal!

Opposite of less than:

Remember: > is the opposite of <

  • a < ba is smaller
  • a > ba is larger
🧠 Manual Logic Approach (Our Solution)

Using if-else to find maximum:

if (a > b)
  max = a;
else
  max = b;

Logic breakdown:

  1. Compare: Is a greater than b?
  2. If YES → a is the larger number, store it in max
  3. If NO → b is larger or equal, store it in max

Why this works:

If a > b is false, it means either:

  • a < b (b is larger) → max = b
  • a == b (they're equal) → max = b ✅ (either works!)

💡 Pattern recognition: This is the exact opposite of finding minimum!

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

Maximum: if (a > b) max = a; else max = b;

⚡ Using max() from <cmath> Library

The shortcut approach:

#include <cmath>
cout << "Maximum is " << max(a, b);

How max() works:

The max(a, b) function compares two values and returns the larger one.

  • max(98, 123) → Returns 123
  • max(0, 68) → Returns 68
  • max(50, 50) → Returns 50
  • max(-10, -5) → Returns -5 (less negative)

One-liner alternative solution:

cout << "Maximum of " << a << " and " << b << " is " << max(a, b);

Pros: Clean, concise, impossible to mess up the logic

📚 Learning value: Understanding the manual approach helps you appreciate how max() works internally

🔄 min() vs max(): Twin Functions

Both min() and max() are part of the <cmath> library and work identically:

Comparison:

  • min(a, b) → Returns the smaller value
  • max(a, b) → Returns the larger value

Example with a=10, b=20:

  • min(10, 20)10
  • max(10, 20)20

Manual equivalents:

// min logic
if (a < b) result = a; else result = b;

// max logic
if (a > b) result = a; else result = b;

💡 Pro tip: You can also chain them!

max(max(a, b), c) → Finds maximum of three numbers!

min(min(a, b), c) → Finds minimum of three numbers!

🔍 Step-by-Step Execution

Example 1: a=98, b=123

  1. Declare variables: long long a, b, max;
  2. Read input: a = 98, b = 123
  3. Check condition: if (98 > 123) → FALSE
  4. Skip if block, go to else: max = b;max = 123
  5. Print: "Maximum of 98 and 123 is 123"

Example 2: a=0, b=68

  1. Declare variables: long long a, b, max;
  2. Read input: a = 0, b = 68
  3. Check condition: if (0 > 68) → FALSE
  4. Skip if block, go to else: max = b;max = 68
  5. Print: "Maximum of 0 and 68 is 68"

Example 3: a=200, b=50

  1. Declare variables: long long a, b, max;
  2. Read input: a = 200, b = 50
  3. Check condition: if (200 > 50) → TRUE
  4. Execute if block: max = a;max = 200
  5. Print: "Maximum of 200 and 50 is 200"

Edge case: a=100, b=100

  1. Check condition: if (100 > 100) → FALSE (equal, not greater)
  2. Go to else: max = b;max = 100
  3. Result is correct! (Either value works when equal)
💭 Comment in Code: Inline Documentation

Notice the helpful comment in our code:

// you could either use max(a,b) from cmath library or

Why this comment is valuable:

  • 📝 Documents alternative approaches
  • 🎓 Educates readers about library functions
  • 💡 Shows awareness of multiple solutions
  • 🔍 Helps others understand your decision-making

Good commenting practices:

  • ✅ Explain why, not just what
  • ✅ Document alternative approaches
  • ✅ Highlight important logic decisions
  • ❌ Don't comment obvious code like // increment i

💡 Pro tip: In interviews, commenting your code shows clear thinking and consideration of alternatives!

🎯 Real-World Applications

Where you'd use maximum finding:

  • 🎮 Gaming: Highest score among players
  • 💰 Finance: Maximum stock price in a period
  • 📊 Analytics: Peak website traffic
  • 🌡️ Weather: Highest temperature of the day
  • 🏆 Sports: Best performance record
  • 📈 Business: Highest sales month

More complex scenarios:

  • Finding maximum in an array of numbers
  • Selecting the best option from multiple choices
  • Determining peak values in data analysis
  • Algorithm optimization (choosing faster approach)

💡 This simple concept is the building block for sorting algorithms, data structures, and optimization problems!

💡 Tip: Master both min and max logic - they're mirror images of each other!

Solution

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    long long a, b, max;
    cin >> a >> b;
    
    // you could either use max(a,b) from cmath library or
    
    if (a > b)
        max = a;
    else
        max = b;
    
    cout << "Maximum of " << a << " and " << b << " is " << max;
    
    return 0;
}