SRM 730, D2, 250-Pointer (IntervalIntersections)

James S. Plank

Thu Aug 30 23:50:20 EDT 2018
You are given two intervals [x1,y1] [x2,y2]. Do yourself a favor and relabel the intervals -- make them [xfirst,yfirst] and [xsecond,ysecond], such that you know that xfirst ≤ xsecond. In example 0, [xfirst,yfirst] = [1,2] and [xsecond,ysecond] = [3,6]. Same for example 1. Doing this makes it easier to reason about the solution.

There are only two choices now -- either (xfirst,yfirst) and (xsecond,ysecond) intersect each other, in which case you'll return 0, or they don't, in which case you'll return xsecond-yfirst.

It is really that simple, and relabeling the intervals helps to make it that simple.