IntervalIntersections
SRM 730 · 2018-02-19 · by lg5293
Problem Statement
For any x ⤠y the interval [x,y] contains all real numbers between x and y, inclusive.
The length of the interval [x,y] is y-x.
Two intervals intersect if they have at least one number in common.
You are given the
These are the endpoints of two intervals: [x1,y1] and [x2,y2].
We are looking for an interval [a,b] that intersects both given intervals.
Return the smallest possible length of the interval [a,b].
Constraints
- x1,y1,x2,y2 will be between 1 and 10^6, inclusive.
- x1 will be less than or equal to y1.
- x2 will be less than or equal to y2.
3 6 1 2 Returns: 1
The two given intervals are [3,6] and [1,2]. The unique shortest interval that intersects both of them is the interval [2,3]. Its length is 3-2 = 1.
1 2 3 6 Returns: 1
The same two intervals as in Example 0, only in different order. The correct return value is the same.
1 10 2 5 Returns: 0
In this test case the optimal length of the interval [a,b] is 0. Examples of such intervals include [2,2] and [4,4].
4 5 1 4 Returns: 0
1 1 1000000 1000000 Returns: 999999
Submissions are judged against all 56 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class IntervalIntersections with a public method int minLength(int x1, int y1, int x2, int y2) · 56 test cases · 2 s / 256 MB per case