Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|cite|improve this question
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
up vote 2 down vote accepted

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.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.