# How many ways can x-y=z with bounds?

I have $x - y = z$ where $z$ is known and I have arbitrary bounds on $x$ and $y$ (lowest and highest possible value for each).

Is there a way, without resorting to brute force, to determine how many possible ways $x-y$ can equal $z$ given the bounds of $x$ and $y$ and known $z$?

-
Did you state your problem correctly? It is trivial to solve as stated. – Harald Hanche-Olsen Oct 11 '12 at 19:43
Unless I am interpreting my own words improperly, it is stated correctly. – John Smith Oct 11 '12 at 19:51

Assuming bounds $x_\min\le x\le x_\max$ and $y_\min\le y\le y_\max$, then $x=y+z$ implies $y_\min+z\le x\le y_\max+z$, and combining these you get $$\max(x_\min,y_\min+z)\le x\le\min(x_\max,y_\max+z)$$ which can happen in $$1+\min(x_\max,y_\max+z)-\max(x_\min,y_\min+z)$$ ways. If this is negative, the correct answer is zero.