Multi-variable Rational Function that is always between the largest and second largest of the arguments. This is a problem I came up for HMMT a few years ago, but I cannot find a solution.

Question: Does there exist a rational function $f(x_1,\cdots, x_n)$ such that for any positive $x_i$, $f(x_1,\cdots, x_n)$ is between the largest and second largest of the $x_i$?

Here is my thought:

*

*For $n = 2$, yes. $f(x_1, x_2) = \frac{x_1 + x_2}{2}$ is such an example.


*For $n \geq 4$, no. We can show that if any two of the arguments are equal, then $f$ must be equal to both of them. Now consider $f(x,x,y,y)$ gives a contradiction.
But I am not sure about the case $n = 3$.
 A: No such function exists for $n=3$.
Write such a function as $p/q$ where $p$ and $q$ are polynomials in $x,y,z$ with  no factors in common, and let $S$ be the intersection of the zero set of $q$ with $\mathbb R_{>0}^3$. Note that, for $(x,y,z)\in S$, $p(x,y,z)=0$, as otherwise the value of $f$ grows without bound near $(x,y,z)$. The rational functions $f(x,x,z)-x$, $f(x,z,x)-x$, and $f(z,x,x)-x$ are each zero for $x\geq z$, and so must each be identically zero.
Now, fix $x_0<z_0$, and consider the rational function $g(y)=f(x_0,y,z_0)$. If $(x_0,x_0,z_0)\not\in S$, then $f$ is differentiable at $(x_0,x_0,z_0)$; in particular, $g$ is differentiable at $x_0$. Since $g(x_0+\epsilon)\geq x_0+\epsilon$ for small $\epsilon>0$, this derivative must be at least $1$. However, this implies that $g(x_0-\epsilon)=x_0-\epsilon+O(\epsilon^2)$, and in particular that $g(x_0-\epsilon)<x_0$ for small enough $\epsilon>0$. However, $g(x_0-\epsilon)=f(x_0,x_0-\epsilon,z_0)\geq x_0$, a contradiction. This means that $(x_0,x_0,z_0)\in S$. In other words, $q(x,x,z)=0$ for all $0<x<z$.
We claim that this implies that $x-y$ divides $q(x,y,z)$ as a polynomial. Indeed, write $q(x,y,z)=(x-y)s(x,y,z)+r(x,z)$ by the division algorithm; then $r(x,z)=0$ for all $0<x<z$. Since $r(x_0,z)$ has infinitely many roots for every $x_0>0$, it is identically $0$ for every $x_0>0$, and thus $r$ is identically $0$.
However, since $S$ is a subset of the zero set of $p$, this argument also implies that $x-y$ divides $p$, contradicting the assumption that $p$ and $q$ share no factors. So, such a rational function $f$ cannot exist.
