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].
Definition
- Class:
- IntervalIntersections
- Method:
- minLength
- Parameters:
- int, int, int, int
- Returns:
- int
- Method signature:
- int minLength(int x1, int y1, int x2, int y2)
- (be sure your method is public)
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.
Examples
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
312348
819554
281345
589030
Returns: 0
179349
507166
358369
738215
Returns: 0
115075
181266
5090
556250
Returns: 0
116437
202335
534613
981260
Returns: 332278
380565
789980
26588
378986
Returns: 1579
240474
615891
606139
945634
Returns: 0
221991
733353
438905
441547
Returns: 0
414616
868217
18462
557668
Returns: 0
219621
985380
102454
212064
Returns: 7557
371857
783935
41429
774080
Returns: 0
630052
660576
870658
987696
Returns: 210082
544929
863609
663688
950181
Returns: 0
528777
755332
544424
629407
Returns: 0
542308
830645
689509
806142
Returns: 0
103397
385741
142326
488476
Returns: 0
177787
322513
463633
604708
Returns: 141120
620240
725668
340787
959517
Returns: 0
369261
387188
72523
231058
Returns: 138203
335311
600705
12574
520766
Returns: 0
302007
866098
319930
915951
Returns: 0
293996
520155
707079
707201
Returns: 186924
564356
932246
734719
883266
Returns: 0
637941
755191
67203
810774
Returns: 0
534290
981522
235934
336049
Returns: 198241
189271
419578
54307
725310
Returns: 0
127050
743717
810708
964488
Returns: 66991
94406
997267
77357
629060
Returns: 0
442630
550870
23906
196016
Returns: 246614
619446
951455
137225
682966
Returns: 0
338957
713770
134447
951653
Returns: 0
1
1000000
1
1000000
Returns: 0
2
4
1
3
Returns: 0
2
11
1
10
Returns: 0
5
6
5
7
Returns: 0
3
6
5
10
Returns: 0
2
4
1
5
Returns: 0
3
5
4
7
Returns: 0
1
3
2
4
Returns: 0
1
6
3
7
Returns: 0
10
12
1
2
Returns: 8
5
10
1
8
Returns: 0
6
8
3
4
Returns: 2
1
4
3
5
Returns: 0
1
5
3
10
Returns: 0
2
3
3
4
Returns: 0
2
3
2
3
Returns: 0
1
5
3
7
Returns: 0
100
200
50
150
Returns: 0
1
10
2
15
Returns: 0
4
10
2
7
Returns: 0
2
5
1
3
Returns: 0