1
$\begingroup$

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$?

  • 0
    Did you state your problem correctly? It is trivial to solve as stated.2012-10-11
  • 0
    Unless I am interpreting my own words improperly, it is stated correctly.2012-10-11

1 Answers 1

2

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.