The Computer Laboratory

Practice Paper 1 Question 5

Using only the functions \(\max\) and \(\min\) and arithmetic operations (no if clauses), express the amount of possible overlap between two intervals \([a_1,a_2]\) and \([b_1,b_2]\), where \(a_1,a_2,b_1,b_2\) are arbitrary real numbers with \(a_1<a_2\) and \(b_1<b_2\).

  • The \(\min\) and \(\max\) functions are applied to two or more numbers, and return the smallest and largest of them respectively, e.g. \(\max(-1,-3)=-1.\)

Hints

  • Hint 1
    Think about intervals on the real number line. Sketch \([a_1,a_2]\) and \([b_1,b_2]\) when these overlap?
  • Hint 2
    What is the left boundary of the overlap in terms of min/max of the 4 numbers?
  • Hint 3
    What about the right boundary?
  • Hint 4
    What if there is no overlap? Remember you can only use min/max functions.

Solution

Assume that intervals overlap, partially or fully, like in the figure above. We can notice that the smallest value in this intersection is always \(\max(a_1, b_1),\) and the largest value in this intersection is \(\min(a_2,b_2).\) Hence, the amount of overlap of these intervals can be expressed as \(\min(a_2,b_2)-\max(a_1, b_1).\)

There will be some overlap only if this value is positive, hence the final formula is \(\max(0,\min(a_2,b_2)-\max(a_1, b_1)).\)

If you have queries or suggestions about the content on this page or the CSAT Practice Platform then you can write to us at oi.footasc@sulp.ecitcarp. Please do not write to this address regarding general admissions or course queries.