Arrange the given 4 numbers in descending order (largest to smallest).
Instead of comparing all pairs, this problem uses a clever elimination strategy:
This reduces the problem complexity significantly!
Input: a=2, b=0, c=4, d=1 Step 1: Find max1 (Largest) max(max(2,0), max(4,1)) max(2, 4) = 4 ✓ Step 2: Find max4 (Smallest) min(min(2,0), min(4,1)) min(0, 1) = 0 ✓ Step 3: Find max2 (First Middle) Check each: Is it NOT max1 AND NOT max4? a=2: NOT 4 AND NOT 0? ✓ → max2 = 2 Step 4: Find max3 (Second Middle) Check each: Is it NOT max1, max4, AND max2? d=1: NOT 4, 0, or 2? ✓ → max3 = 1 Output: 4 > 2 > 1 > 0
Accept four integer as a input
Print the output as "max1 > max2 > max3 > max4"
-10^15 <= INPUT <= 10^15
#include <iostream> #include <algorithm> // for min() and max() using namespace std; int main() { long long a, b, c, d; cin >> a >> b >> c >> d; long long max1 = max(max(a, b), max(c, d)); // largest long long max4 = min(min(a, b), min(c, d)); // smallest // For middle two, pick the remaining numbers long long max2, max3; // Assign remaining numbers if ((a != max1 && a != max4)) max2 = a; else if ((b != max1 && b != max4)) max2 = b; else if ((c != max1 && c != max4)) max2 = c; else max2 = d; if ((a != max1 && a != max4 && a != max2)) max3 = a; else if ((b != max1 && b != max4 && b != max2)) max3 = b; else if ((c != max1 && c != max4 && c != max2)) max3 = c; else max3 = d; cout << max1 << " > " << max2 << " > " << max3 << " > " << max4 << endl; return 0; }
Why This Approach is Clever:
// Traditional approach: Compare all pairs (6 comparisons) // a vs b, a vs c, a vs d, b vs c, b vs d, c vs d // Elimination approach: Find extremes, then exclude them // Step 1: Find largest (2 comparisons in nested max) // Step 2: Find smallest (2 comparisons in nested min) // Step 3: Find middle values by elimination // This is more intuitive and easier to understand!
How Elimination Works:
// Given: a=2, b=0, c=4, d=1 // max1 = 4, max4 = 0 // Finding max2: if ((a != max1 && a != max4)) // Is a NOT 4 AND NOT 0? max2 = a; // Yes! a=2 ✓ // Finding max3: if ((a != max1 && a != max4 && a != max2)) // Is a NOT 4, 0, or 2? max3 = a; // No, a=2 is max2 else if ((b != max1 && b != max4 && b != max2)) // Is b NOT 4, 0, or 2? max3 = b; // No, b=0 is max4 else if ((c != max1 && c != max4 && c != max2)) // Is c NOT 4, 0, or 2? max3 = c; // No, c=4 is max1 else max3 = d; // Yes! d=1 ✓
Finding the Largest of Four Numbers:
// max() only works with 2 arguments at a time // To find max of 4 numbers: nest the calls max1 = max(max(a, b), max(c, d)); ↓ max( max(a,b) , max(c,d) ) ↓ ↓ max of first max of second two numbers two numbers ↓ Final: max of both results // Example: a=2, b=0, c=4, d=1 max1 = max(max(2, 0), max(4, 1)) = max(2, 4) = 4 ✓
Finding the Smallest of Four Numbers:
// Same logic with min() max4 = min(min(a, b), min(c, d)); // Example: a=2, b=0, c=4, d=1 max4 = min(min(2, 0), min(4, 1)) = min(0, 1) = 0 ✓
Why Variable Names are max1, max2, max3, max4:
Case 1: All Numbers Equal
// Input: 5 5 5 5 max1 = 5 // largest max4 = 5 // smallest (same as largest!) // Finding max2: none of a,b,c,d satisfy (x != 5 && x != 5) // Falls through to else: max2 = d = 5 ✓ // Finding max3: similarly falls through to else: max3 = 5 ✓ // Output: 5 > 5 > 5 > 5
Case 2: Two Numbers Equal
// Input: 3 3 1 5 max1 = 5 max4 = 1 // Both a and b are 3 (neither is max1 or max4) // First if catches: max2 = a = 3 // Second if-else finds: max3 = b = 3 // Output: 5 > 3 > 3 > 1 ✓
Case 3: Negative Numbers
// Input: -5 -10 -2 -8 max1 = max(max(-5, -10), max(-2, -8)) = max(-5, -2) = -2 // least negative = largest ✓ max4 = min(min(-5, -10), min(-2, -8)) = min(-10, -8) = -10 // most negative = smallest ✓ // Output: -2 > -5 > -8 > -10
Method | Pros | Cons |
---|---|---|
Elimination Logic (Current) |
• Intuitive • Educational • Good for learning |
• More lines of code • Manual logic |
Array + std::sort() | • Shortest code • Professional • Scalable |
• Uses advanced concepts • Less educational |
Bubble Sort | • Classic algorithm • Educational |
• Overkill for 4 numbers • More complex |
Manual If-Else | • No functions needed • Complete control |
• Many comparisons • Error-prone • Hard to maintain |
Initializer List | • Modern C++ • Clean syntax |
• Doesn't give individual sorted values • Only max/min |
Using std::sort() (Professional Approach):
#include <iostream> #include <algorithm> using namespace std; int main() { long long arr[4]; for(int i=0; i<4; i++) cin >> arr[i]; sort(arr, arr+4, greater<long long>()); // descending cout << arr[0] << " > " << arr[1] << " > " << arr[2] << " > " << arr[3] << endl; return 0; }
Aspect | Ascending Order | Descending Order |
---|---|---|
Direction | Smallest → Largest | Largest → Smallest |
Symbol | < (less than) | > (greater than) |
Example | 1 < 2 < 3 < 4 | 4 > 3 > 2 > 1 |
First Position | Minimum value | Maximum value |
Last Position | Maximum value | Minimum value |
Common Use | Default sorting Finding minimums |
Rankings Top scores Leaderboards |