Solution 1. We begin by providing an example of a function f for which there are two values of g(x). We take the function f(x)=⌊x⌋−{x}, where ⌊x⌋ denotes the floor of x (that is, the largest integer less than or equal to x) and {x}=x−⌊x⌋ denotes the fractional part of x.
First, we show that f satisfies P(x,y). Given x,y∈Q, we have
f(x)+y=⌊x⌋−{x}+⌊y⌋+{y}=(⌊x⌋+⌊y⌋)+({y}−{x})x+f(y)=⌊x⌋+{x}+⌊y⌋−{y}=(⌊x⌋+⌊y⌋)+({x}−{y})
If {x}<{y}, then we have that the fractional part of f(x)+y is {y}−{x} and the floor is ⌊x⌋+⌊y⌋, so f(x)+y→x+f(y). Likewise, if {x}>{y}, then x+f(y)→f(x)+y. Finally, if {x}={y}, then f(x)+y=x+f(y)=⌊x⌋+⌊y⌋ is an integer. In all cases, the relation P is satisfied.
Finally, we observe that if x is an integer then g(x)=0, and if x is not an integer then g(x)=−2, so there are two values for g(x) as required.
Now, we prove that there cannot be more than two values of g(x). P(x,x) tells us that x+f(x)∼x+f(x), or in other words, for all x,
f(x+f(x))=x+f(x)(1)
We begin with the following lemma.
Lemma 1. f is a bijection, and satisfies
f(−f(−x))=x(2)
Proof. We first prove that f is injective. Suppose that f(x1)=f(x2); then P(x1,x2) tells us that f(x1)+x2∼f(x2)+x1. Without loss of generality, suppose that f(x1)+x2→f(x2)+x1.
But f(x1)=f(x2), so f(f(x1)+x2)=f(f(x2)+x2)=f(x2)+x2 by (1). Therefore, f(x2)+x1=f(x2)+x2, as required.
Now, (1) with x=0 tells us that f(f(0))=f(0) and so by injectivity f(0)=0.
Applying P(x,−f(x)) tells us that 0∼x+f(−f(x)), so either 0=f(0)=x+f(−f(x)) or f(x+f(−f(x)))=0 which implies that x+f(−f(x))=0 by injectivity. Either way, we deduce that x=−f(−f(x)), or x=f(−f(−x)) by replacing x with −x.
Finally, note that bijectivity follows immediately from (2).
Since f is bijective, it has an inverse, which we denote f−1. Rearranging (2) (after replacing x with −x) gives that f(−x)=−f−1(x). We have g(x)=f(x)+f(−x)=f(x)−f−1(x).
Suppose g(x)=u and g(y)=v, where u=v are both nonzero. Define x′=f−1(x) and y′=f−1(y); by definition, we have
x′→x→x′+uy′→y→y′+v.
Putting in P(x′,y) gives x+y∼x′+y′+v, and putting in P(x,y′) gives x+y∼x′+y′+u. These are not equal since u=v, and x+y may have only one incoming and outgoing arrow because f is a bijection, so we must have either x′+y′+u→x+y→x′+y′+v or the same with the arrows reversed. Swapping (x,u) and (y,v) if necessary, we may assume without loss of generality that this is the correct direction for the arrows.
Also, we have −x′−u→−x→−x′ by Lemma 1. Putting in P(x+y,−x′−u) gives y∼y′+v−u, and so y′+v−u must be either y′+v or y′. This means u must be either 0 or v, and this contradicts our assumption about u and v.
Solution 2. We again start with Lemma 1, and note f(0)=0 as in the proof of that lemma.
P(x,−f(y)) gives x+f(−f(y))∼f(x)−f(y), and using (2) this becomes x−y∼f(x)−f(y). In other words, either f(x−y)=f(x)−f(y) or x−y=f(f(x)−f(y)). In the latter case, we deduce that
f(−(x−y))f(y−x)=f(−f(f(x)−f(y)))=f(−f(f(x)−f(y)))=f(y)−f(x)
Thus, f(y)−f(x) is equal to either f(y−x) or −f(x−y). Replacing y with x+d, we deduce that f(x+d)−f(x)∈{f(d),−f(−d)}.
Now, we prove the following claim.
Claim. For any n∈Z>0 and d∈Q, we have that either g(d)=0 or g(d)=±g(d/n).
In particular, if g(d/n)=0 then g(d)=0.
Proof. We first prove that if g(d/n)=0 then g(d)=0. Suppose that g(d/n)=0. Then f(d/n)=−f(−d/n) and so f(x+d/n)−f(x)=f(d/n) for any x. Applying this repeatedly, we deduce that f(x+d)−f(x)=nf(d/n) for any x. Applying this with x=0 and x=−d and adding gives f(d)+f(−d)=0, so g(d)=0, and in particular the claim is true whenever g(d)=0.
Now, select n∈Z>0 and d∈Q such that g(d)=0, and observe that we must have g(d/n)=0. Observe that for any k∈Z we have that f(kd/n)−f((k−1)d/n)∈{f(d/n),−f(−d/n)}. Let Ai be the number of k∈Z with i−n<k⩽i such that this difference equals f(d/n).
We deduce that for any i∈Z,
f(id/n)−f(id/n−d)=i−n<k⩽i∑f(kd/n)−f((k−1)d/n)=Aif(d/n)−(n−Ai)f(−d/n)=−nf(−d/n)+Aig(d/n)
Since g(d/n) is nonzero, this is a nonconstant linear function of Ai. However, there are only two possible values for f(id/n)−f(id/n−d), so there must be at most two possible values for Ai as i varies. And since Ai+1−Ai∈{−1,0,1}, those two values must differ by 1 (if there are two values).
Now, we have
f(d)−f(0)f(0)−f(−d)=−nf(−d/n)+Ang(d/n),and=−nf(−d/n)+A0g(d/n)
Subtracting these (using the fact that f(0)=0 ) we obtain
f(d)+f(−d)=(An−A0)g(d/n)=±g(d/n)
where the last line follows from the fact that g(d) is nonzero.
It immediately follows that there can only be one nonzero number of the form g(x) up to sign; to see why, if g(d) and g(d′) are both nonzero, then for some n,n′∈Z>0 we have d/n=d′/n′. But
g(d)=±g(d/n)=±g(d′)
Finally, suppose that for some d,d′ we have g(d)=c and g(d′)=−c for some nonzero c. So we have
f(d)+f(−d)−f(d′)−f(−d′)=2c
which rearranges to become (f(d)−f(d′))−(f(−d′)−f(−d))=2c.
Each of the bracketed terms must be equal to either f(d−d′) or −f(d′−d). However, they cannot be equal since c is nonzero, so g(d−d′)=f(d−d′)+f(d′−d)=±2c. This contradicts the assertion that g(−x)=±c for all x.
Solution 3. As in Solution 1, we start by establishing Lemma 1 as above, and write f−1(x)=−f(−x) for the inverse of f, and g(x)=f(x)−f−1(x).
We now prove the following.
Lemma 2. If g(x)=g(y), then g(x+y)=±(g(x)−g(y)).
Proof. Assume x and y are such that g(x)=g(y). Applying P(x,f−1(y)) gives x+y∼f(x)+f−1(y), and applying P(f−1(x),y) gives x+y∼f−1(x)+f(y).
Observe that
(f(x)+f−1(y))−(f−1(x)+f(y))=(f(x)−f−1(x))−(f(y)−f−1(y))=g(x)−g(y)
By assumption, g(x)=g(y), and so f(x)+f−1(y)=f−1(x)+f(y). Since f is bijective, this means that these two values must be f(x+y) and f−1(x+y) in some order, and so g(x+y)=f(x+y)−f−1(x+y) must be their difference up to sign, which is either g(x)−g(y) or g(y)−g(x).
Claim. If x and q are rational numbers such that g(q)=0 and n is an integer, then g(x+nq)=g(x).
Proof. If g(b)=0 and g(a)=g(a+b), then the lemma tells us that g(b)=±(g(a+b)−g(a)), which contradicts our assumptions. Therefore, g(a)=g(a+b) whenever g(b)=0.
A simple induction then gives that g(nb)=0 for any positive integer n, and g(nb)=0 for negative n as g(x)=g(−x). The claim follows immediately.
Lemma 3. There cannot be both positive and negative elements in the range of g.
Proof. Suppose that g(x)>0 and g(y)<0. Let S be the set of numbers of the form mx+ny for integers m,n. We first show that g(S) has infinitely many elements. Indeed, suppose g(S) is finite, and let a∈S maximise g and b∈S maximise −g. Then a+b∈S, and g(a+b)=g(a)−g(b) or g(b)−g(a). In the first case g(a+b)>g(a) and in the second case g(a+b)<g(b); in either case we get a contradiction.
Now, we show that there must exist some nonzero rational number q with g(q)=0. Indeed, suppose first that a+f(a)=0 for all a. Then g(a)=f(a)+f(−a)=0 for all a, and so g takes no nonzero value. Otherwise, there is some a with a+f(a)=0, and so (1) yields that f(q)=0 for q=a+f(a)=0. Noting that f(−q)=0 from Lemma 1 tells us that g(q)=0, as required.
Now, there must exist integers s and s′ such that xs=qs′ and integers t and t′ such that yt=qt′. The claim above gives that the value of g(mx+ny) depends only on the values of mmods and nmodt, so g(mx+ny) can only take finitely many values.
Finally, suppose that g(x)=u and g(y)=v where u=v have the same sign. Assume u,v>0 (the other case is similar) and assume u>v without loss of generality.
P(f−1(x),f−1(y)) gives x−y∼f−1(x)−f−1(y)=f(x)−f(y)−(u−v), and P(x,y) gives x−y∼f(x)−f(y). u−v is nonzero, so f(x−y) and f−1(x−y) must be f(x)−f(y)−(u−v) and f(x)−f(y) in some order, and since g(x−y) must be nonnegative, we have
f(x)−f(y)−(u−v)→x−y→f(x)−f(y).
Then, P(x−y,f−1(y)) tells us that (x−y)+y∼(f(x)−f(y))+(f(y)−v), so x∼f(x)−v, contradicting either v=u or v>0.
Answer: 2 is the maximum number of elements.