Given three integers, find maximum and minimum number between them without using conditional statements or ternary operator.
Approach 1: (Using short-circuiting in Boolean expressions)
The idea is to take advantage of short-circuiting in Boolean expressions. We know that in Boolean AND operation such as x && y
, y is evaluated only if x is true. If x is false, then y is not evaluated, because the whole expression would be false which can be deduced without even evaluating y. This is called short-circuiting in Boolean expressions.
The idea is to apply this principle to the below code. Initially max is a. Now if (max < b)
is true, then that means, b is greater than a so the second sub-expression (max = b)
is evaluated and max is set to b. If however (max < b)
is false, then second sub-expression will not be evaluated and max will remain a (which is greater than b). In a similar fashion, the second expression is evaluated.
We can implement minimum function as well in similar fashion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> using namespace std; int maximum(int a, int b, int c) { // initialize max with a int max = a; // set max to b if and only if max is less than b (max < b) && (max = b); // these are not conditional statements. // set max to c if and only if max is less than c (max < c) && (max = c); // these are just Boolean expressions. return max; } int minimum(int a, int b, int c) { // initialize min with a int min = a; // set min to b if and only if min is more than b (min > b) && (min = b); // set min to c if and only if min is more than c (min > c) && (min = c); return min; } // main function int main() { cout << maximum(7, 9, 4) << endl; cout << minimum(6, 3, 9); return 0; } |
Approach 2: (Using array index)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> using namespace std; int maximum(int a, int b, int c) { // arr1 will contain first two elements int arr1[] = { a, b }; // arr2 will contain maximum of first two elements at 0 index // and third element at index 1 int arr2[] = { arr1[a < b], c }; // finally return the maximum element return arr2[arr2[0] < c]; } int minimum(int a, int b, int c) { // arr1 will contain first two elements int arr1[] = { a, b }; // arr2 will contain minimum of first two elements at 0 index // and third element at index 1 int arr2[] = { arr1[a > b], c }; // finally return the minimum element return arr2[arr2[0] > c]; } // main function int main() { cout << maximum(6, 3, 9) << endl; cout << minimum(6, 3, 9); return 0; } |
Approach 3: (Approach 2 Simplified)
We can simplify approach 2 by breaking the problem into finding maximum/minimum of two numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; int maximum(int a, int b) { int lookup[] = {a, b}; return lookup[a < b]; } int maximum (int a, int b, int c) { return maximum(a, maximum(b, c)); } // main function int main() { cout << maximum(6, 3, 9) << endl; return 0; } |
We can implement minimum function as well in similar fashion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; int minimum(int a, int b) { int lookup[] = {a, b}; return lookup[a > b]; } int minimum(int a, int b, int c) { return minimum(a, minimum(b, c)); } // main function int main() { cout << minimum(6, 3, 9) << endl; return 0; } |
Approach 4: (Using repeated subtraction)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> using namespace std; int minimum (int a, int b, int c) { int min = 0; while (a && b && c) a--, b--, c--, min++; return min; } int maximum (int a, int b, int c) { int max = 0; while (a > 0 || b > 0 || c > 0) a--, b--, c--, max++; return max; } // main function int main() { cout << maximum(6, 3, 9) << endl; cout << minimum(6, 3, 9); return 0; } |
Thanks for reading.
Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding :)
Leave a Reply
#include <iostream>
using std::cout;
using std::endl;
template<typename T0, typename T1>
auto max(T0 a, T1 b) {
return 1./2. * (a + b + abs(a - b));
}
template<typename T0, typename T1, typename T2>
auto max3(T0 a, T1 b, T2 c) {
return max(a, max(b, c));
}
int main() {
cout << "max(2.001, 2): " << max(2.001, 2) << endl;
cout << "max(2, 1): " << max(2, 1) << endl;
cout << "max(2, 2): " << max(2, 2) << endl;
cout << "max(0, 0): " << max(0, 0) << endl;
cout << "max(-1, -10): " << max(-1, -10) << endl;
cout << "max(-1, 0): " << max(-1, 0) << endl;
cout << "max(1, -10): " << max(1, -10) << endl;
cout << "max3(2.001, 2, 4): " << max3(2.001, 2, 4) << endl;
cout << "max3(2, 1, 0): " << max3(2, 1, 0) << endl;
cout << "max3(2, 2, -1): " << max3(2, 2, -1) << endl;
cout << "max3(0, 0, 0): " << max3(0, 0, 0) << endl;
cout << "max3(-1, -10, -3): " << max3(-1, -10, -3) << endl;
cout << "max3(-1, 0, 10): " << max3(-1, 0, 10) << endl;
cout << "max3(1, -10, -2): " << max3(1, -10, -2) << endl;
}