A service records n uptime intervals on a timeline, each a half-open range from its start up to (but not including) its end. Intervals that overlap or touch (one ends exactly where the next begins) form a single continuous uptime stretch. After joining all such intervals, report the length of the longest continuous stretch.
Line 1: an integer n.
Next n lines: two integers start and end describing one interval.
A single integer: the length of the longest continuous merged stretch.
Example 1
Input
4 1 4 3 6 9 11 20 21
Expected
5
Explanation
[1,4) and [3,6) merge into [1,6) of length 5; the others give lengths 2 and 1. The longest stretch is 5.
Example 2
Input
3 0 2 2 4 4 5
Expected
5
Explanation
Each interval touches the next, chaining into one stretch [0,5) of length 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →