Is it possible to find the i'th largest number using only min and max functions?

93 Views Asked by At

If we are only able to use min and max which each take two integer arguments and return the lowest and highest integer, respectively, is it possible to construct a function that will return the i'th largest integer from a set of, say, four integers?

For example, the largest is easy. Given the integers $\{a, b, c, d\}$, finding the largest is expressed as $\max(\max(a,b), \max(c,d))$. A similar example exists for the smallest.

Is it impossible to find the second largest using only max and min?

1

There are 1 best solutions below

1
On BEST ANSWER

The second largest number in $\{a,b,c,d\}$ is given by $$\min(\max(a,b,c),\max(a,b,d),\max(a,c,d),\max(b,c,d))$$ since the maximum of the four numbers appears in three of the four $\max$ functions, so the overall minimum comes from the remaining $\max$ function, which takes the maximum of all other values, thus returning the second largest. You can easily reduce the above expression to use only two arguments per $\max,\min$ as you already noticed, but that makes it less clear to read, so I did not do that.

This can easily be generalized, the $k$-th largest number of a finite set $A$ of numbers is given by $$\min\left\{\max(B)\ |\ B\subseteq A, |B|=|A|-k+1\right\}$$