A blueprint places two axis-aligned rectangular rooms on an integer grid. Each rectangle is given by its lower-left corner (x1, y1) and its upper-right corner (x2, y2) with x1 < x2 and y1 < y2. Print the area of the region covered by both rectangles at once. If they do not overlap, or only touch along an edge or corner, the overlapping area is 0.
Line 1: four integers x1 y1 x2 y2 for the first rectangle.
Line 2: four integers x1 y1 x2 y2 for the second rectangle.
A single integer: the area of the intersection.
Example 1
Input
0 0 4 4 2 1 6 3
Expected
4
Explanation
The overlap spans x from 2 to 4 (width 2) and y from 1 to 3 (height 2), so its area is 2 * 2 = 4.
Example 2
Input
0 0 2 2 3 3 5 5
Expected
0
Explanation
The rectangles are far apart and share no interior, so the overlap area is 0.
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 →