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++:

Download  Run Code

Approach 2: Using array index

Download  Run Code

 
We can simplify the above approach by breaking the problem into finding the maximum/minimum of two numbers. The following C++ program demonstrates it:

Download  Run Code

 
We can implement the minimum function, in a similar fashion, as demonstrated below in C++:

Download  Run Code

Approach 3: Using repeated subtraction

Download  Run Code

 
References: https://stackoverflow.com/questions/7074010/find-maximum-of-three-number-in-c-without-using-conditional-statement-and-ternar