Find maximum and minimum value of a triplet without using a conditional statement
Given three integers, find the 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 operations such as x && y, y is only evaluated 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 following code. Initially, max is a. If max < b is true, then that means b is greater than a, so the second subexpression max = b is evaluated, and max is set to b. If, however, max < b is false, then the second subexpression is not evaluated, and max will remain a (greater than b). Similarly, the second expression is evaluated.
We can implement the minimum function as well, in a similar fashion, as demonstrated below in C++:
|
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 |
#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; } int main() { cout << maximum(7, 9, 4) << endl; cout << minimum(6, 3, 9) << endl; 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 |
#include <iostream> using namespace std; int maximum(int a, int b, int c) { // `first` will contain the first two elements int first[] = { a, b }; // `second` will contain the maximum of the first two elements at // the 0th index and the third element at index 1 int second[] = { first[a < b], c }; // finally, return the maximum element return second[second[0] < c]; } int minimum(int a, int b, int c) { // `first` will contain the first two elements int first[] = { a, b }; // `second` will contain the minimum of the first two elements at the // 0th index and the third element at index 1 int second[] = { first[a > b], c }; // finally, return the minimum element return second[second[0] > c]; } int main() { cout << maximum(6, 3, 9) << endl; cout << minimum(6, 3, 9) << endl; return 0; } |
We can simplify the above approach by breaking the problem into finding the maximum/minimum of two numbers. The following C++ program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#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)); } int main() { cout << maximum(6, 3, 9) << endl; return 0; } |
We can implement the minimum function, in a similar fashion, as demonstrated below in C++:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#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)); } int main() { cout << minimum(6, 3, 9) << endl; return 0; } |
Approach 3: 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 30 |
#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; } int main() { cout << maximum(6, 3, 9) << endl; cout << minimum(6, 3, 9) << endl; return 0; } |
Check if a number is even or odd without using any conditional statement
Find minimum number without using conditional statement or ternary operator
Find maximum number without using conditional statement or ternary operator
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)