QUESTION #024

Find Maximum of Three Numbers

Easy

📋 Problem Statement

Find the maximum value among three given numbers.

🎯 Comparison Logic

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

  • Compare each number with the other two
  • The number that is larger than both others is the maximum
  • Use logical AND (&&) to check both conditions simultaneously
  • The logic is the inverse of finding minimum (use > instead of <)
Finding Maximum: Progressive Comparison
Numbers:  a = 100,  b = 200,  c = 1000

Step 1: Is a > b AND a > c?
        100 > 200? NO  →  a is not maximum

Step 2: Is b > a AND b > c?
        200 > 100? YES  BUT  200 > 1000? NO  →  b is not maximum

Step 3: Else clause
        Neither a nor b is maximum  →  c must be maximum!

Result: Maximum is 1000

📥 Input Format

Accept three integer values as a input

📤 Output Format

Print the output as "Maximum is ___"

💡 Examples

Example 1:
Input: 100 200 1000
Output: Maximum is 1000
Analysis:
• a = 100, b = 200, c = 1000
• Check a: 100 > 200? NO → not maximum
• Check b: 200 > 100? YES, but 200 > 1000? NO → not maximum
• Else: c must be the maximum
Result: Maximum is 1000
Example 2:
Input: 10 20 30
Output: Maximum is 30
Analysis:
• a = 10, b = 20, c = 30
• Check a: 10 > 20? NO → not maximum
• Check b: 20 > 10? YES, but 20 > 30? NO → not maximum
• Else: c = 30 is the maximum
Result: Maximum is 30
Example 3:
Input: 999 50 75
Output: Maximum is 999
Analysis:
• a = 999, b = 50, c = 75
• Check a: 999 > 50? YES, 999 > 75? YES → maximum found!
Result: Maximum is 999
Example 4 (Equal Values):
Input: 50 50 30
Output: Maximum is 50
Analysis:
• a = 50, b = 50, c = 30
• Check a: 50 > 50? NO (equal, not greater) → not maximum
• Check b: 50 > 50? NO, 50 > 30? YES → not both true
• Else: Assigns c, but the maximum value is still correct (50)
Note: When values are equal, the else clause is triggered, but one of the equal values is the correct maximum.
Example 5 (Negative Numbers):
Input: -5 -10 -3
Output: Maximum is -3
Analysis:
• a = -5, b = -10, c = -3
• Remember: -3 is larger than -5 and -10
• Check a: -5 > -10? YES, -5 > -3? NO → not maximum
• Check b: -10 > -5? NO → not maximum
• Else: c = -3 is the maximum
Result: Maximum is -3

⚠️ Constraints

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

✅ Solution

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

using namespace std;

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

⚠️ About the Header Files

The code includes <cmath>, but the max() 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 greater than (>) to compare values
  • Logical AND (&&): Both conditions must be true for a number to be maximum
  • If-Else-If Chain: Testing conditions sequentially until one is true
  • Else Clause: Handles remaining case when neither a nor b is maximum
  • Inverse Logic: Maximum finding is the opposite of minimum finding
  • Long Long Data Type: Handling very large values (up to 10^15)
  • Alternative Methods: Built-in max() function vs manual comparison

💡 Understanding the Logic Flow

Decision Tree:

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

Why the Else Clause Works:

  • If a is not greater than both others → it's not maximum
  • If b is not greater than both others → it's not maximum
  • Therefore, c must be the maximum (or equal to maximum)
  • This process of elimination works even when values are equal

Min vs Max Comparison:

Aspect Finding Minimum Finding Maximum
Operator < (less than) > (greater than)
Condition a < b && a < c a > b && a > c
Logic Smaller than both Larger than both

🚀 Using the Built-in max() Function

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

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

using namespace std;

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

How it works:

  • max(a, b) → returns the larger of a and b
  • max(result, c) → compares that result with c
  • Final result is the maximum of all three

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

long long maximum = max({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.

⚠️ Common Mistakes to Avoid

  • Incomplete Comparisons: Only checking one condition
    // ❌ WRONG: Only checks if a > b, not if a > c
    if (a > b)
        maximum = a;  // What if c is larger than a?
  • Using OR instead of AND: Would give wrong results
    // ❌ WRONG: a must be larger than BOTH b and c
    if (a > b || a > c)  // Using OR is incorrect!
        maximum = a;  // This would choose a even if c is largest
  • Mixing up operators: Using < when you need >
  • Copy-paste errors: Copying minimum code and forgetting to change operators
  • Not handling equal values: Logic should work even when numbers are equal
  • Data type overflow: Using int instead of long long for large values

🎯 Alternative Approaches

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

long long maximum = a;  // Assume a is maximum

if (b > maximum)
    maximum = b;  // Update if b is larger

if (c > maximum)
    maximum = c;  // Update if c is larger

cout << "Maximum is " << maximum;

Advantage: Cleaner, more intuitive logic. Easy to extend to more numbers.

Approach 2: Ternary Operator (Nested)

long long maximum = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

cout << "Maximum is " << maximum;

Note: Compact but harder to read. Not recommended for beginners.

Approach 3: Using Array and Loop (Scalable)

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

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

cout << "Maximum is " << maximum;

Advantage: Easily scalable to any number of values.

🌍 Real-world Applications

  • E-commerce: Finding the highest bidder in auctions
  • Gaming: Determining highest score, strongest character stats
  • Finance: Identifying peak stock prices, maximum profit
  • Data Analysis: Finding maximum values in datasets
  • Sports: Determining winning scores and records
  • Resource Management: Finding server with maximum capacity
  • Weather Systems: Tracking maximum temperatures, peak rainfall
  • Performance Monitoring: Identifying maximum CPU/memory usage
  • Quality Control: Finding maximum acceptable thresholds
  • Route Planning: Selecting longest/furthest route options

🎓 Combining Min and Max

You can find both minimum and maximum in a single program:

#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    long long a, b, c;
    cin >> a >> b >> c;
    
    // Method 1: Using built-in functions
    long long minimum = min({a, b, c});
    long long maximum = max({a, b, c});
    
    cout << "Minimum is " << minimum << endl;
    cout << "Maximum is " << maximum << endl;
    cout << "Range: " << (maximum - minimum) << endl;
    
    return 0;
}

Useful Derived Values:

  • Range: maximum - minimum (spread of values)
  • Average: (a + b + c) / 3
  • Middle Value: a + b + c - minimum - maximum
  • Sum of Extremes: minimum + maximum

🎓 Extension Challenges

  • Level 1: Find maximum of 4 or 5 numbers
  • Level 2: Find both minimum AND maximum in one program
  • Level 3: Find the second largest number among three
  • Level 4: Find maximum of N numbers (user inputs N)
  • Level 5: Find maximum and its position (which input was it?)
  • Level 6: Calculate the range (difference between max and min)
  • Level 7: Sort three numbers in descending order
  • Bonus: Find max, min, and middle value simultaneously

🎯 What You'll Learn

  • Understanding comparison logic with greater-than operator
  • Using logical AND (&&) to combine multiple conditions
  • Building if-else-if chains for sequential checks
  • Recognizing inverse relationships (min vs max logic)
  • Working with built-in library functions (max)
  • Choosing optimal approaches for different scenarios
  • Handling edge cases (equal values, negative numbers)
  • Understanding process of elimination in logic

💪 Practice Tips

  • Test All Positions: Try cases where maximum is first, second, third position
  • Test Equal Values: (50, 50, 30), (50, 30, 50), (30, 50, 50), (50, 50, 50)
  • Test Negative Numbers: (-5, -10, -3), (-1, -2, -3), (-100, -200, -50)
  • Test Zero: (0, 5, -5), (0, 0, 0), (10, 0, -10)
  • Test Large Values: Use values close to 10^15 and -10^15
  • Compare with Minimum: Write both programs side-by-side to see patterns
  • Trace Logic: Manually walk through conditions with different inputs
  • Try All Methods: Implement manual logic, built-in function, and simplified approach

🔗 Connection to Previous Problem

This problem is the complementary pair to Question #23 (Find Minimum):

Aspect Find Minimum (#23) Find Maximum (#24)
Goal Find smallest value Find largest value
Operator < >
Function min() max()
Logic Smaller than both Larger than both
Use Case Lowest price, shortest time Highest score, longest distance

Key Insight: Understanding one makes the other trivial - just flip the comparison operator!