Problems > Segment Overlap

Segment Overlap

Difficulty: 1000 2000 ms 128 MB

math

Two closed segments [A, B] and [C, D] lie on a number line. Print the length of their overlap (0 if they do not intersect).

Input: one line with four integers A B C D (A ≤ B, C ≤ D).

Output: the overlap length: max(0, min(B, D) − max(A, C)).

Constraints:

  • −109 ≤ A ≤ B ≤ 109
  • −109 ≤ C ≤ D ≤ 109

Sample Tests

Input 1
1 5 3 8
Output 1
2
Input 2
1 2 5 6
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.