← March 2025 questions

MARCH 2025 / ADVANCED / QUESTION 2

Question 2

Archived question

Try the question in your own editor, then check your output below.

THE QUESTION

Text from the official booklet. Refer to the PDF for original mathematical notation, formatting, and linked input files. Printed samples and inputs are reproduced as published.

A long straight wall needs to be painted, and two painters have been assigned the task. The first
painter paints the interval between x=a and x=b. For example, if a=3 and b=5, then the painter
paints an interval of length 2. The other painter paints the interval from x=c to x=d, which may
possibly overlap with part or all of Farmer John's interval. Please determine the total length of
fence that is now covered with paint.
The first line of the input contains the integers a and b, separated by a space (a<b).
The second line contains integers c and d, separated by a space (c<d).
The values of a, b, c, and d all lie in the range 0…100, inclusive.
Please output a single line containing the total length of the fence covered with paint.
SAMPLE INPUT:
7 10
4 8
SAMPLE OUTPUT:
6
Input
Case 1:
52 76
23 52
Case 2:
10 72
33 72

CHECK YOUR ANSWER

Try your answer.

Run your program in your own editor, then paste its output below. This checks the stated output; it does not assess your full program. Practice attempts do not affect competition scores.

Use the corresponding input from the question. For lists, brackets and commas are optional for numeric answers; text lists may also be entered one item per line.

Use the corresponding input from the question. For lists, brackets and commas are optional for numeric answers; text lists may also be entered one item per line.

Reveal worked answer

Case 1

53

Case 2

62

Add the interval lengths, subtracting their overlap once.

Reveal supplied answers

From the supplied Advanced answer key. Outputs are reproduced as provided.

Case 1

53

Case 2

62

Source: GIC 2025 Advanced Questions and the supplied Advanced answer key, March 2025 archive.