Find the maximum value among three given numbers.
To find the maximum of three numbers a, b, and c:
>
instead of <
)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
Accept three integer values as a input
Print the output as "Maximum is ___"
-10^15 <= INPUT <= 10^15
#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; }
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()
.
>
) to compare valuesmax()
function vs manual comparisonDecision 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:
a
is not greater than both others → it's not maximumb
is not greater than both others → it's not maximumc
must be the maximum (or equal to maximum)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 |
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 bmax(result, c)
→ compares that result with cC++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.
// ❌ WRONG: Only checks if a > b, not if a > c if (a > b) maximum = a; // What if c is larger than a?
// ❌ 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
<
when you need >
int
instead of long long
for large valuesApproach 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.
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:
maximum - minimum
(spread of values)(a + b + c) / 3
a + b + c - minimum - maximum
minimum + maximum
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!