A formula which gives the maximum of a series of numbers This formula gives the maximum of 3 numbers:
$$\frac{a}{2} + \frac{b}{4} + \frac{c}{4} + \frac{|b-c|}{4} + \frac{1}{2}\left|a -\frac{b}{2} - \frac{c}{2} - \frac{|b-c|}{2}\right| = \max(a,b,c)$$
I've found this over the internet, I have no idea how can one develop such a formula, and I wonder how.
What would it look like for 4 numbers ? and 5 etc.
Is it possible to have formula which gives the maximum of a series of $n$ numbers?
 A: Let's start with $2$ numbers. The best way to see this is to imagine the numbers on the number line.
We find the midpoint of the $2$ numbers: $\frac{a+b}{2}$.
Next, we can find half the distance of the $2$ numbers: $\frac{|a-b|}{2}$
Adding them up, we have $\max(a, b)=\frac{a+b}{2}+\frac{|a-b|}{2}$.
The formula you have for $3$ numbers is found by expanding $\max(a, \max(b, c))$.
You can continue with any number of variables, if we want to find the maximum of $a_1, a_2, a_3, \dots, a_n$, we can evaluate it slowly:
$$\max(a_1, \max(a_2, \max(a_3, \dots\max(a_{n-1}, a_n)\dots)))$$
Because this post may take up too much space if I added too many expressions, I would write the expression for $n=5$:
$\frac{a_{1}}{2}+\frac{1}{2}\left(\frac{a_{2}}{2}+\frac{1}{2}\left(\frac{a_{3}}{2}+\frac{1}{2}\left(\frac{a_{4}}{2}+\frac{1}{2}\left(a_{5}\right)+\frac{1}{2}\left|a_{4}-a_{5}\right|\right)+\frac{1}{2}\left|a_{3}-\frac{a_{4}}{2}-\frac{1}{2}\left(a_{5}\right)-\frac{1}{2}\left|a_{4}+a_{5}\right|\right|\right)+\frac{1}{2}\left|a_{2}-\frac{a_{3}}{2}-\frac{1}{2}\left(\frac{a_{4}}{2}+\frac{1}{2}\left(a_{5}\right)+\frac{1}{2}\left|a_{4}-a_{5}\right|\right)-\frac{1}{2}\left|a_{3}+\frac{a_{4}}{2}-\frac{1}{2}\left(a_{5}\right)-\frac{1}{2}\left|a_{4}+a_{5}\right|\right|\right|\right)+\frac{1}{2}\left|a_{1}-\frac{a_{2}}{2}-\frac{1}{2}\left(\frac{a_{3}}{2}+\frac{1}{2}\left(\frac{a_{4}}{2}+\frac{1}{2}\left(a_{5}\right)+\frac{1}{2}\left|a_{4}-a_{5}\right|\right)+\frac{1}{2}\left|a_{3}-\frac{a_{4}}{2}-\frac{1}{2}\left(a_{5}\right)-\frac{1}{2}\left|a_{4}+a_{5}\right|\right|\right)-\frac{1}{2}\left|a_{2}+\frac{a_{3}}{2}-\frac{1}{2}\left(\frac{a_{4}}{2}+\frac{1}{2}\left(a_{5}\right)+\frac{1}{2}\left|a_{4}-a_{5}\right|\right)-\frac{1}{2}\left|a_{3}+\frac{a_{4}}{2}-\frac{1}{2}\left(a_{5}\right)-\frac{1}{2}\left|a_{4}+a_{5}\right|\right|\right|\right|$
Here I include the C++ program used to generate the expression in $\LaTeX$.
#include <cstdio>
#include <algorithm>
using namespace std;
/**
Prints out the maximum of the variables
a_{printed+1}...a_n
sign is used to switch the + and - signs.
**/
void printMax(int n, int printed, int sign) {
    if (printed+1 == n) {
        printf("a_{%d}", n);
        return;
    }
    char p = '+', m = '-';
    if (sign < 0) p = '-', m = '+';
    printf("\\frac{a_{%d}}{2}%c\\frac{1}{2}\\left(", printed+1, p);
    if (printed<n) printMax(n, printed+1, 1);
    printf("\\right)%c\\frac{1}{2}\\left|a_{%d}%c", p, printed+1, m);
    if (printed<n) printMax(n, printed+1, -1);
    printf("\\right|");
}
int main(void) {
    //freopen("maxClosedFormOut.txt", "w", stdout);
    int N;
    scanf("%d", &N);
    printf("$");
    printMax(5, 0, 1);
    printf("$");
    return 0;
}

