Rectangle Area solution and explanation
9 more videos on this problem
leetcode 223 Rectangle AreaCodebix · 10:31 · 6K views
223. Rectangle AreaTech Adora by Nivedita · 11:24 · 3K views
LeetCode in Python 223. Rectangle Area - Michelle小梦想家Michelle小梦想家 · 7:44 · 1K views
Daily Leetcode #961: Nov 17, 2022 - 223. Rectangle AreaProgramming Live with Larry · 10:47 · 1K views
223. Rectangle Area || DSA || Leetcode Daily Challenge ||Leetcode Daily Challenge · 8:41 · 1K views
Leetcode : 223. Rectangle Area (Java & JavaScript)ktsuw_21 · 13:28 · 541 views
LeetCode 223 | Rectangle Area | OpHaxor SolutionAlgorithms & Programming Interview Prep · 12:10 · 372 views
Sneaky Sorts! – Rectangle Area – Leetcode 223Aaron Codes · 15:23 · 343 views
223. Rectangle Area - English VersionSimple Code - 단순코드 · 8:32 · 302 views
Facebook Interview Question - Rectangle Area- Leetcode 223- PythonSephorus · 3:24 · 178 views
The problem
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:
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.
def computeArea(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2):
area1 = (ax2 - ax1) * (ay2 - ay1)
area2 = (bx2 - bx1) * (by2 - by1)
overlapWidth = min(ax2, bx2) - max(ax1, bx1)
overlapHeight = min(ay2, by2) - max(ay1, by1)
overlapArea = 0
if overlapWidth > 0 and overlapHeight > 0:
overlapArea = overlapWidth * overlapHeight
return area1 + area2 - overlapArea
print(computeArea(-3, 0, 3, 4, 0, -1, 9, 2))
print(computeArea(-2, -2, 2, 2, -2, -2, 2, 2))public class Solution {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int area1 = (ax2 - ax1) * (ay2 - ay1);
int area2 = (bx2 - bx1) * (by2 - by1);
int overlapWidth = Math.min(ax2, bx2) - Math.max(ax1, bx1);
int overlapHeight = Math.min(ay2, by2) - Math.max(ay1, by1);
int overlapArea = 0;
if (overlapWidth > 0 && overlapHeight > 0) {
overlapArea = overlapWidth * overlapHeight;
}
return area1 + area2 - overlapArea;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.computeArea(-3, 0, 3, 4, 0, -1, 9, 2));
System.out.println(sol.computeArea(-2, -2, 2, 2, -2, -2, 2, 2));
}
}#include <iostream>
int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int area1 = (ax2 - ax1) * (ay2 - ay1);
int area2 = (bx2 - bx1) * (by2 - by1);
int overlapWidth = std::min(ax2, bx2) - std::max(ax1, bx1);
int overlapHeight = std::min(ay2, by2) - std::max(ay1, by1);
int overlapArea = 0;
if (overlapWidth > 0 && overlapHeight > 0) {
overlapArea = overlapWidth * overlapHeight;
}
return area1 + area2 - overlapArea;
}
int main() {
std::cout << computeArea(-3, 0, 3, 4, 0, -1, 9, 2) << std::endl;
std::cout << computeArea(-2, -2, 2, 2, -2, -2, 2, 2) << std::endl;
return 0;
}function computeArea(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) {
const area1 = (ax2 - ax1) * (ay2 - ay1);
const area2 = (bx2 - bx1) * (by2 - by1);
const overlapWidth = Math.min(ax2, bx2) - Math.max(ax1, bx1);
const overlapHeight = Math.min(ay2, by2) - Math.max(ay1, by1);
let overlapArea = 0;
if (overlapWidth > 0 && overlapHeight > 0) {
overlapArea = overlapWidth * overlapHeight;
}
return area1 + area2 - overlapArea;
}
console.log(computeArea(-3, 0, 3, 4, 0, -1, 9, 2));
console.log(computeArea(-2, -2, 2, 2, -2, -2, 2, 2));#include <stdio.h>
int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int area1 = (ax2 - ax1) * (ay2 - ay1);
int area2 = (bx2 - bx1) * (by2 - by1);
int overlapWidth = ((ax2 < bx2) ? ax2 : bx2) - ((ax1 > bx1) ? ax1 : bx1);
int overlapHeight = ((ay2 < by2) ? ay2 : by2) - ((ay1 > by1) ? ay1 : by1);
int overlapArea = 0;
if (overlapWidth > 0 && overlapHeight > 0) {
overlapArea = overlapWidth * overlapHeight;
}
return area1 + area2 - overlapArea;
}
int main() {
printf("%d\n", computeArea(-3, 0, 3, 4, 0, -1, 9, 2));
printf("%d\n", computeArea(-2, -2, 2, 2, -2, -2, 2, 2));
return 0;
}using System;
class Solution {
public int ComputeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int area1 = (ax2 - ax1) * (ay2 - ay1);
int area2 = (bx2 - bx1) * (by2 - by1);
int overlapWidth = Math.Min(ax2, bx2) - Math.Max(ax1, bx1);
int overlapHeight = Math.Min(ay2, by2) - Math.Max(ay1, by1);
int overlapArea = 0;
if (overlapWidth > 0 && overlapHeight > 0) {
overlapArea = overlapWidth * overlapHeight;
}
return area1 + area2 - overlapArea;
}
static void Main() {
Solution solution = new Solution();
Console.WriteLine(solution.ComputeArea(-3, 0, 3, 4, 0, -1, 9, 2));
Console.WriteLine(solution.ComputeArea(-2, -2, 2, 2, -2, -2, 2, 2));
}
}This Python script computes the area covered by two rectangles. It does so by calculating the total of each rectangle's area, then finding and subtracting any overlapping area. This approach uses straightforward arithmetic operations to achieve the result.
The approach runs in constant time, O(1), since only basic arithmetic is used, with a space complexity of O(1) due to the limited number of variables involved.
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.
def is_overlapping(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2):
return not (ax1 >= bx2 or bx1 >= ax2 or ay1 >= by2 or by1 >= ay2)
def computeArea(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2):
area1 = (ax2 - ax1) * (ay2 - ay1)
area2 = (bx2 - bx1) * (by2 - by1)
overlap_area = 0
if is_overlapping(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2):
overlap_width = min(ax2, bx2) - max(ax1, bx1)
overlap_height = min(ay2, by2) - max(ay1, by1)
overlap_area = overlap_width * overlap_height
return area1 + area2 - overlap_area
print(computeArea(-3, 0, 3, 4, 0, -1, 9, 2))
print(computeArea(-2, -2, 2, 2, -2, -2, 2, 2))public class Solution {
public boolean isOverlapping(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
return !(ax1 >= bx2 || bx1 >= ax2 || ay1 >= by2 || by1 >= ay2);
}
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int area1 = (ax2 - ax1) * (ay2 - ay1);
int area2 = (bx2 - bx1) * (by2 - by1);
int overlapArea = 0;
if (isOverlapping(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2)) {
int overlapWidth = Math.min(ax2, bx2) - Math.max(ax1, bx1);
int overlapHeight = Math.min(ay2, by2) - Math.max(ay1, by1);
overlapArea = overlapWidth * overlapHeight;
}
return area1 + area2 - overlapArea;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.computeArea(-3, 0, 3, 4, 0, -1, 9, 2));
System.out.println(sol.computeArea(-2, -2, 2, 2, -2, -2, 2, 2));
}
}#include <iostream>
bool isOverlapping(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
return !(ax1 >= bx2 || bx1 >= ax2 || ay1 >= by2 || by1 >= ay2);
}
int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int area1 = (ax2 - ax1) * (ay2 - ay1);
int area2 = (bx2 - bx1) * (by2 - by1);
int overlapArea = 0;
if (isOverlapping(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2)) {
int overlapWidth = std::min(ax2, bx2) - std::max(ax1, bx1);
int overlapHeight = std::min(ay2, by2) - std::max(ay1, by1);
overlapArea = overlapWidth * overlapHeight;
}
return area1 + area2 - overlapArea;
}
int main() {
std::cout << computeArea(-3, 0, 3, 4, 0, -1, 9, 2) << std::endl;
std::cout << computeArea(-2, -2, 2, 2, -2, -2, 2, 2) << std::endl;
return 0;
}function isOverlapping(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) {
return !(ax1 >= bx2 || bx1 >= ax2 || ay1 >= by2 || by1 >= ay2);
}
function computeArea(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) {
const area1 = (ax2 - ax1) * (ay2 - ay1);
const area2 = (bx2 - bx1) * (by2 - by1);
let overlapArea = 0;
if (isOverlapping(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2)) {
const overlapWidth = Math.min(ax2, bx2) - Math.max(ax1, bx1);
const overlapHeight = Math.min(ay2, by2) - Math.max(ay1, by1);
overlapArea = overlapWidth * overlapHeight;
}
return area1 + area2 - overlapArea;
}
console.log(computeArea(-3, 0, 3, 4, 0, -1, 9, 2));
console.log(computeArea(-2, -2, 2, 2, -2, -2, 2, 2));
#include <stdio.h>
#include <algorithm>
bool isOverlapping(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
// If one rectangle is to the left of other or one is above other.
if (ax1 >= bx2 || bx1 >= ax2 || ay1 >= by2 || by1 >= ay2)
return false;
return true;
}
int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int area1 = (ax2 - ax1) * (ay2 - ay1);
int area2 = (bx2 - bx1) * (by2 - by1);
int overlapArea = 0;
if (isOverlapping(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2)) {
int overlapWidth = std::min(ax2, bx2) - std::max(ax1, bx1);
int overlapHeight = std::min(ay2, by2) - std::max(ay1, by1);
overlapArea = overlapWidth * overlapHeight;
}
return area1 + area2 - overlapArea;
}
int main() {
printf("%d\n", computeArea(-3, 0, 3, 4, 0, -1, 9, 2));
printf("%d\n", computeArea(-2, -2, 2, 2, -2, -2, 2, 2));
return 0;
}using System;
class Solution {
public bool IsOverlapping(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
return !(ax1 >= bx2 || bx1 >= ax2 || ay1 >= by2 || by1 >= ay2);
}
public int ComputeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int area1 = (ax2 - ax1) * (ay2 - ay1);
int area2 = (bx2 - bx1) * (by2 - by1);
int overlapArea = 0;
if (IsOverlapping(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2)) {
int overlapWidth = Math.Min(ax2, bx2) - Math.Max(ax1, bx1);
int overlapHeight = Math.Min(ay2, by2) - Math.Max(ay1, by1);
overlapArea = overlapWidth * overlapHeight;
}
return area1 + area2 - overlapArea;
}
static void Main() {
Solution solution = new Solution();
Console.WriteLine(solution.ComputeArea(-3, 0, 3, 4, 0, -1, 9, 2));
Console.WriteLine(solution.ComputeArea(-2, -2, 2, 2, -2, -2, 2, 2));
}
}In Python, a helper function detects overlap by checking coordinate limits. Overlap adjustments are made when rectangles overlap, resulting in the total area covered after subtracting the duplicated overlap.
This solution runs in O(1) time and uses O(1) space, leveraging direct calculations and minimum condition checks.
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).
function computeArea(
ax1: number,
ay1: number,
ax2: number,
ay2: number,
bx1: number,
by1: number,
bx2: number,
by2: number,
): number {
const a = (ax2 - ax1) * (ay2 - ay1);
const b = (bx2 - bx1) * (by2 - by1);
const width = Math.min(ax2, bx2) - Math.max(ax1, bx1);
const height = Math.min(ay2, by2) - Math.max(ay1, by1);
return a + b - Math.max(width, 0) * Math.max(height, 0);
}func computeArea(ax1 int, ay1 int, ax2 int, ay2 int, bx1 int, by1 int, bx2 int, by2 int) int {
a := (ax2 - ax1) * (ay2 - ay1)
b := (bx2 - bx1) * (by2 - by1)
width := min(ax2, bx2) - max(ax1, bx1)
height := min(ay2, by2) - max(ay1, by1)
return a + b - max(height, 0)*max(width, 0)
}Complexity comparison
| Approach | Complexity |
|---|---|
| Calculate Areas Separately and Subtract Overlap | The approach runs in constant time, O(1), since only basic arithmetic is used, with a space complexity of O(1) due to the limited number of variables involved. |
| Coordinate Sweep Method | This solution runs in O(1) time and uses O(1) space, leveraging direct calculations and minimum condition checks. |
| Calculate Overlapping Area | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Calculate Areas Separately and Subtract Overlap | O(1) | O(1) | Best general solution for two rectangles. Simple coordinate math and minimal implementation. |
| Coordinate Sweep Method | O(1) | O(1) | Useful for understanding how rectangle union problems scale to many rectangles using sweep-line techniques. |
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.
