Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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

1 Answer 1

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|improve this answer

Your Answer

 
discard

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.