How does one DERIVE the formula for the maximum of two numbers [duplicate]

I want to derive (not prove that this is true) the formula

$\max (x,y) = \dfrac{x + y + |y-x|}{2}$

I was reading a proof (which they have the result ahead of time already) that we do cases and then we consider $x + y + |y - x|$. I am not sure how they first came up with $x + y + |y - x|$ in the first place


Solution 1:

The value halfway between x and y is $$\frac{x+y}{2}.$$ If we want to add something to it to get to $\max \left( x,y \right)$, we would need to add the difference between $\max \left( x,y \right)$ and $\frac{x+y}{2}$. What is that value? well if $\max \left( x,y \right) = y$ then it is $$y- \frac{x+y }{2} = \frac{y-x }{2},$$ and if $\max \left( x,y \right) = x$ then it is $$x- \frac{x+y }{2} = \frac{x-y}{2}.$$

We know 2 things:

  1. $x-y$ and $y-x$ are the same up to a change of sign.
  2. the difference between the max and the middle is positive.

So,

$$\left| \frac{x-y}{2} \right|=\left| \frac{y-x}{2} \right|,$$

and whether $\max \left( x,y \right)$ is $x$ or $y$, you can add this amount to the value halfway between $x$ and $y$ to get $\max \left( x,y \right)$.

This gives you the formula:

$$\max \left( x,y \right) = \frac{x+y + \left| x-y \right|}{2}.$$