TITLE: Problem Solving Question With Polynomials
QUESTION [7 upvotes]: For any polynomial $p$ with real coefficients, let
  $$
S(p):= \{x\in \mathbb{R} \mid p(x) \in \mathbb{Z}\}
$$
  Prove that if $p$, $q$ are two polynomials such that $S(p) = S(q)$, then either $p+q$ or $p-q$ is a constant.

My attempt:
Let $S:=S(p)=S(q)$, if $x\in S:$ $$p(x)+q(x) \in Z, p(x)-q(x)\in \mathbb{Z}$$
By adding these equations we can receive that $p(x)\in \mathbb{Z}$, which is pointless info.
I also tried:
$$
p^2\in \mathbb{Z}, q^2\in \mathbb{Z} \implies p^2-q^2\in \mathbb{Z}\implies (p+q)(p-q)\in \mathbb{Z}
$$
which leads to nowhere.
I'm not sure how to even look at this question, since even if I do manage to find that $p+q$ or $p-q$ are constants for every $x\in S$, I wouldn't know how to apply that knowledge to every $x\in \mathbb{R}$.

REPLY [2 votes]: Not sure it's worth writing this separately (doesn't fit into a comment), but here's a bit more legwork and detail filling before applying Sly's idea
For any non-constant polynomial $p$, $S(p)$ is infinite. (why?)
0). The case where either $p$ or $q$ is constant is easy (if $p$ is constant, then $S(p)$ is either empty or equal to $\mathbb{R}$, and thus so is $S(q)$; in the first case, with the above remark $q$ is constant; in the latter,then $q$ can take only integer values; but then as a polynomial is continuous this means it can only take one value, otherwise it would take all values in between as well).
1). Assuming now both $p$ and $q$ are non-constant. Note that wlog, we can assume the leading coefficient $a$ of $p$ is positive (as otherwise, we can just consider $-p$ and $-q$. Furthermore, we can $(\dagger)$ also assume the same about the leading coefficient $b$ of $q$, as the hypotheses hold for both $p,q$ and $p,-q$. Finally, again without loss of generality, we can assume $a > b$ (otherwise, just swap $p$ and $q$. We can now apply Sly's idea (note that the conclusion will be that $p-q$ is constant; the case $p+q$ constant would come from the discussion above, where there is a $(\dagger)$: by possibly swapping $q$ wth $-q$).