Skip to main content

Rectangle Area - Solution & Explanation

MediumMathGeometry20 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.

The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).

The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).

 

Example 1:

Rectangle Area
Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
Output: 45

Example 2:

Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
Output: 16

 

Constraints:

  • -104 <= ax1 <= ax2 <= 104
  • -104 <= ay1 <= ay2 <= 104
  • -104 <= bx1 <= bx2 <= 104
  • -104 <= by1 <= by2 <= 104

Approach Overview

Problem Overview: Two axis-aligned rectangles are defined by their bottom-left and top-right coordinates. The task is to compute the total area covered by both rectangles. If the rectangles overlap, the overlapping region must only be counted once.

Approach 1: Calculate Areas Separately and Subtract Overlap (O(1) time, O(1) space)

Each rectangle’s area is computed independently using the standard formula (x2 - x1) * (y2 - y1). Adding these two values double-counts any overlapping region. The fix is to compute the overlap width and height using coordinate comparisons: overlapWidth = max(0, min(ax2, bx2) - max(ax1, bx1)) and overlapHeight = max(0, min(ay2, by2) - max(ay1, by1)). Multiply them to get the overlapping area and subtract it from the sum of both rectangles. This solution relies purely on coordinate arithmetic, making it extremely efficient with constant O(1) time and O(1) space. The logic mainly uses geometric reasoning from Math and coordinate comparisons from Geometry.

The key insight is that overlap occurs only when projections on both the x-axis and y-axis intersect. Using min and max isolates the intersection boundaries. The max(0, ...) guard prevents negative values when rectangles do not intersect. This approach is compact, easy to implement, and handles all cases including disjoint rectangles, partial overlap, and one rectangle fully inside another.

Approach 2: Coordinate Sweep Method (O(1) time, O(1) space)

A coordinate sweep views the rectangles as vertical spans along the x-axis. First determine the combined x-intervals where rectangles exist. For each overlapping x segment, compute the active y coverage by intersecting the rectangles’ vertical ranges. The covered area becomes width * unionHeight. While sweep-line techniques usually appear in problems involving many rectangles, the same idea applies here conceptually with just two rectangles.

This approach emphasizes interval reasoning rather than direct subtraction. You determine horizontal overlap first, then compute the effective vertical span that contributes to area. Although the final complexity is still O(1), it mirrors how large-scale rectangle union problems are solved with sweep-line algorithms and interval merging. Understanding this method strengthens intuition for more advanced geometry problems involving multiple regions.

Recommended for interviews: The expected solution is the area calculation with overlap subtraction. Interviewers want to see you identify rectangle intersection using min/max comparisons and avoid negative overlap values. A sweep-style explanation demonstrates deeper geometric thinking, but the direct arithmetic approach shows the cleanest reasoning and implementation under time pressure.

Approach 1: Calculate Areas Separately and Subtract Overlap

This approach involves calculating the area of each rectangle separately, then finding the overlapping area, and subtracting it from the total. First, calculate the area of each rectangle by using the formula width multiplied by height. To find the overlap, determine the maximum of the bottom-left x and y coordinates, and the minimum of the top-right x and y coordinates. If the rectangles do intersect, subtract the overlap area from the sum of the rectangle areas to get the total covered area.

This program first computes the rectangles' areas using their width and height. It then calculates the overlap width and height based on intersecting limits of x and y coordinates. If these intersections are valid (width and height are positive), it then computes the overlap area and subtracts it from the total area of both rectangles. Finally, it outputs the total area covered by the rectangles.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

The time complexity is O(1) since it involves basic arithmetic operations, and the space complexity is also O(1) as no additional space is needed beyond a few variables.

Try this approach in the editor →

Approach 2: Coordinate Sweep Method

By using the coordinate sweep method, we can iterate through all points in a uniform grid covering both rectangles. This technique conceptualizes sweeping lines across the plane, adding and subtracting areas as intersections are counted. Use sorting and a plane-sweep technique to determine overlapping intervals more effectively.

This C variant checks if two rectangles can overlap by verifying bounding constraints. The rectangles' areas are computed, and the overlapping area is considered zero if there is no overlap. Otherwise, overlapping dimensions are calculated, and their areas are subtracted accordingly.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

The time complexity remains O(1) since it involves constant time computation, while the space complexity is also O(1) because it uses a constant number of variables.

Try this approach in the editor →

Approach 3: Calculate Overlapping Area

First, we calculate the area of the two rectangles separately, denoted as a and b. Then we calculate the overlapping width width and height height. The overlapping area is max(width, 0) times max(height, 0). Finally, we subtract the overlapping area from a and b.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Calculate Areas Separately and Subtract Overlap

The time complexity is O(1) since it involves basic arithmetic operations, and the space complexity is also O(1) as no additional space is needed beyond a few variables.

Coordinate Sweep Method

The time complexity remains O(1) since it involves constant time computation, while the space complexity is also O(1) because it uses a constant number of variables.

Calculate Overlapping Area—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Calculate Areas Separately and Subtract OverlapO(1)O(1)Best general solution for two rectangles. Simple coordinate math and minimal implementation.
Coordinate Sweep MethodO(1)O(1)Useful for understanding how rectangle union problems scale to many rectangles using sweep-line techniques.

Video Solution

leetcode 223 Rectangle Area • Codebix • 6,585 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Rectangle Area easy or hard?
Rectangle Area is usually classified as a Medium problem. The arithmetic is simple, but many candidates make mistakes handling the overlap calculation or negative intersection values. Correctly using min and max comparisons is the key insight.
How to solve Rectangle Area in O(1)?
Compute area1 = (ax2 - ax1) * (ay2 - ay1) and area2 = (bx2 - bx1) * (by2 - by1). Then calculate overlapWidth = max(0, min(ax2, bx2) - max(ax1, bx1)) and overlapHeight = max(0, min(ay2, by2) - max(ay1, by1)). Multiply them to get the overlap area and subtract it from the sum of the two rectangle areas.
What is the best approach for Rectangle Area?
The most efficient approach computes each rectangle's area separately and subtracts the overlapping region. You find overlap width using min and max of the x-coordinates and overlap height using the y-coordinates. The final area is areaA + areaB - overlapArea. This runs in O(1) time and uses O(1) space.
What data structure is used in Rectangle Area?
The problem does not require complex data structures. It mainly uses coordinate arithmetic and comparison operations from geometry and math. In generalized versions with many rectangles, sweep-line algorithms and interval structures may be used.
What is the time complexity of Rectangle Area?
The optimal solution runs in O(1) time because it only performs a fixed number of arithmetic operations and coordinate comparisons. Space complexity is also O(1) since no additional data structures are required.
Rectangle Area Python or Java solution approach?
Python and Java implementations follow the same logic: compute both rectangle areas, calculate overlap width and height using min and max operations, then subtract the overlap from the total. The code typically fits in a few lines and runs in constant time.
Is Rectangle Area asked at Google, Amazon, or Meta interviews?
Rectangle Area appears in coding interview preparation lists for companies like Amazon, Google, and Meta because it tests geometric reasoning and careful boundary handling. The problem is commonly used to check whether candidates can reason about coordinate overlap conditions.

Ready to solve this problem?

Practice Rectangle Area with our built-in code editor and test cases.

Practice on FleetCode