Ctrl K

Area Under Two Rectangles

Medium
Two rectangles with sides parallel to the axes lie on a plane. The first is given by its lower-left corner (a_left, a_bottom) and its upper-right corner (a_right, a_top); the second by its corners (b_left, b_bottom) and (b_right, b_top). Return the area they cover together, counting any overlap only once.
Example 1
a_left=0, a_bottom=0, a_right=4, a_top=3, b_left=2, b_bottom=1, b_right=6, b_top=5 → 24

The areas are 12 and 16, and they share the square from (2, 1) to (4, 3) of area 4. In total 12 + 16 - 4 = 24.

Example 2
a_left=-3, a_bottom=-3, a_right=-1, a_top=-1, b_left=1, b_bottom=0, b_right=2, b_top=5 → 9

The rectangles do not touch: 4 + 5 = 9.

Example 3
a_left=0, a_bottom=0, a_right=10, a_top=10, b_left=2, b_bottom=2, b_right=5, b_top=5 → 100
Constraints
  • -10⁴ ≤ every coordinate ≤ 10⁴
  • a_left ≤ a_right, a_bottom ≤ a_top
  • b_left ≤ b_right, b_bottom ≤ b_top
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNo Inward Corners MediumFour Pins Make a Square MediumLaser in a Mirror Room MediumSmallest Paddock MediumSmallest Rectangle of Pins Medium
loading Python…
Input
a_left=0, a_bottom=0, a_right=4, a_top=3, b_left=2, b_bottom=1, b_right=6, b_top=5
Expected
24

Run checks the first 3 tests in your browser. Submit checks all 77 tests on the server, hidden ones included.