Rectangle Area

Given the coordinates of two axis-aligned rectangles in a 2D plane, return the total area covered by the two rectangles. Rectangle A has bottom-left corner (ax1, ay1) and top-right corner (ax2, ay2); rectangle B has bottom-left (bx1, by1) and top-right (bx2, by2).

30:00

Rectangle Area
medium
Free written solution
Topics
Companies

Given the coordinates of two axis-aligned rectangles in a 2D plane, return the total area covered by the two rectangles. Rectangle A has bottom-left corner (ax1, ay1) and top-right corner (ax2, ay2); rectangle B has bottom-left (bx1, by1) and top-right (bx2, by2).

Example 1:
Input: {"ax1":-3,"ay1":0,"ax2":3,"ay2":4,"bx1":0,"by1":-1,"bx2":9,"by2":2}
Output: 45
Constraints:
  • −104≤ax1≤ax2≤104-10^4 \leq ax1 \leq ax2 \leq 10^4

  • −104≤ay1≤ay2≤104-10^4 \leq ay1 \leq ay2 \leq 10^4

  • −104≤bx1≤bx2≤104-10^4 \leq bx1 \leq bx2 \leq 10^4

  • −104≤by1≤by2≤104-10^4 \leq by1 \leq by2 \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"ax1":-3,"ay1":0,"ax2":3,"ay2":4,"bx1":0,"by1":-1,"bx2":9,"by2":2}

Rectangle A is 6 wide and 4 tall, so it covers 6 × 4 = 24.

■ A (-3,0)→(3,4)■ B (0,-1)→(9,2)■ overlap
xy-39A
area_a
24
area_b
-
overlap_w
-
overlap_h
-
Variables
VariableValue
area_a24
area_b-
overlap_w-
overlap_h-
DepthFunction Call
Stack empty
0/6