How math help reduce terms and conditions of someone's dying wish? Good morning everyone... This is my very first question here, so I apologise in advance for any wrongdoing which I possibly make unintentionally. So here is a little background story. I'm working at a law firm and there's a case of distributing someone's assets after he died to his 5 children from 3 different wives. Luckily the case has been settled, but the problem is still bugging me. I'll try to describe the problem as clearly as possible but I'm not going into detail about this case (although I'm pretty sure that you can have some wild guesses which might be right, haha).

Suppose X1, X2, Y1, Y2 > 0; X1 > X2; and Y1 > Y2.
  How to simplify expressions of Z which satisfy the following
  conditions:
  
  
*
  
*If X2 < X1 < Y2 < Y1, then Z = 0
  
*If Y2 < Y1 < X2 < X1, then Z = 0
  
*If X2 < Y2 < X1 < Y1, then Z = X1 - Y2
  
*If Y2 < X2 < X1 < Y1, then Z = X1 - X2
  
*If Y2 < X2 < Y1 < X1, then Z = Y1 - X2
  
*If X2 < Y2 < Y1 < X1, then Z = Y1 - Y2

The goal is to reduce the expressions of Z yet still satisfy the above conditions. I've tried to scratch around for the simpler expressions during my off days, but none of which are correct. Is it possible to do this (I begin to think not)? If it's possible, then you can imagine how much terms and conditions that can be reduce in this legal document (although in the real world might not be applied). Thank you so much for your kind response.
 A: Let's take a look at the cases 3-6. In the case 3 & 4, the first number that you need to calculate (i.e. the number from which you subtract the other one) is $X_1$. If we compare it to the table of inequalities, this is the case if $X_1 < Y_1$. In the case 5 & 6, $Y_1$ needs to be calculated, which corresponds to the case $Y_1 < X_1$. More simply, the first number is $\min(X_1, Y_1)$.
A similar consideration shows that the number you subtract is $\max(X_2,Y_2)$.
This means that a formula to simplify the cases 3-6 would be $\min(X_1, Y_1) - \max(X_2, Y_2)$.
Now let's see what happens if we also apply this formula to the first two cases. In the first case we get $X_1 - Y_2$ and in the second case $Y_1 - X_2$. Now how can we distinguish this from the cases 3-6? Well, our formula so far yields a positive value in the cases 3-6 and a negative value for the cases 1 & 2. This means if our formula yields a negative value, we just need to return $0$. This can all be accomplished in one simple formula:
$$\max(\min(X_1, Y_1) - \max(X_2, Y_2), 0)$$
