Rectangle Overlap

Easy Arrays & Hashing Math / Number Theory

Rectangle Overlap is a easy arrays & hashing problem solved with the math / number theory pattern. The best approach, optimal (overlap of the two shadows), runs in O(1) time and O(1) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from rule out the four ways to be apart up.

Problem

An axis-aligned rectangle is given as a list [x1, y1, x2, y2], where (x1, y1) is its bottom-left corner and (x2, y2) is its top-right corner. Its edges are parallel to the x and y axes.

Two rectangles overlap when the region they share has positive area. Rectangles that only touch along an edge or at a corner do not overlap.

Given two such rectangles rec1 and rec2, return true if they overlap and false otherwise.

Examples

Example 1

Input
rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output
true
Why
The square from (1,1) to (2,2) lies inside both rectangles, and it has area 1.

Example 2

Input
rec1 = [0,0,1,1], rec2 = [1,0,2,1]
Output
false
Why
The rectangles meet along the line x = 1, but a shared edge has zero area, so they do not overlap.

Example 3

Input
rec1 = [0,0,1,1], rec2 = [2,2,3,3]
Output
false
Why
The second rectangle lies entirely to the right of the first, with a gap between them.

Constraints

  • rec1.length == 4 and rec2.length == 4
  • -10^9 <= rec1[i], rec2[i] <= 10^9
  • Each rectangle has positive area: x1 < x2 and y1 < y2

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (3 examples, or your own). Submit also runs 14 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Rule out the four ways to be apartO(1)O(1)
Optimal (overlap of the two shadows)O(1)O(1)

1Rule out the four ways to be apart

TimeO(1)Four comparisons.
SpaceO(1)No extra storage.

Two rectangles have no shared area exactly when one of them lies fully on one side of the other: to the left, to the right, below, or above. Touching counts as apart, so each test uses <=. If none of the four holds, they overlap.

  1. rec1 is left of rec2 when rec1's right edge is at or before rec2's left edge: rec1[2] <= rec2[0].
  2. rec1 is right of rec2 when rec2[2] <= rec1[0].
  3. rec1 is below rec2 when rec1[3] <= rec2[1], and above it when rec2[3] <= rec1[1].
  4. Return true only if none of the four holds.
class Solution {
    public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
        boolean left = rec1[2] <= rec2[0];
        boolean right = rec2[2] <= rec1[0];
        boolean below = rec1[3] <= rec2[1];
        boolean above = rec2[3] <= rec1[1];
        return !(left || right || below || above);
    }
}

2Optimal (overlap of the two shadows)

TimeO(1)A few max, min and comparisons.
SpaceO(1)No extra storage.

Project both rectangles onto the x-axis: each one casts an interval from its left edge to its right edge. The shared part of two intervals runs from the larger start to the smaller end, and it has positive length only when that start is strictly before that end. The rectangles share area exactly when their x-shadows and their y-shadows both share positive length, because the shared region is the shared x-stretch times the shared y-stretch.

  1. Shared x-stretch: from max(rec1[0], rec2[0]) to min(rec1[2], rec2[2]). It is real when the start is less than the end.
  2. Shared y-stretch: from max(rec1[1], rec2[1]) to min(rec1[3], rec2[3]), with the same test.
  3. Return true when both stretches are real. There is no need to compute the shared area: with coordinates as large as 10^9, width times height would overflow an int.
class Solution {
    public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
        boolean xShared = Math.max(rec1[0], rec2[0]) < Math.min(rec1[2], rec2[2]);
        boolean yShared = Math.max(rec1[1], rec2[1]) < Math.min(rec1[3], rec2[3]);
        return xShared && yShared;
    }
}

Edge cases to test

  • Rectangles that share only an edge (false)
  • Rectangles that share only a corner (false)
  • One rectangle completely inside the other (true)
  • Identical rectangles (true)
  • A cross shape: overlap in the middle even though no corner of either rectangle is inside the other (true)
  • Overlap on the x-axis but a gap on the y-axis, or the other way round (false)
  • Coordinates near plus or minus 10^9: the overlap's width times its height can reach 4 × 10^18, far past a 32-bit int, so test the two axes separately instead of computing an area

Hints

Hint 1

It is easier to describe when two rectangles do NOT overlap. Where can one rectangle sit relative to the other so that they share no area?

Hint 2

Look at the shadows of the rectangles on the x-axis. They are two intervals. If the intervals do not overlap, can the rectangles?

Hint 3

Two intervals [a1, a2] and [b1, b2] share a stretch of positive length exactly when max(a1, b1) < min(a2, b2). The rectangles overlap when this holds on both axes.

FAQ

What is the best time complexity for Rectangle Overlap?

Optimal (overlap of the two shadows) runs in O(1) time and O(1) extra space. A few max, min and comparisons.

Which pattern does Rectangle Overlap use?

It is a arrays & hashing problem that uses the math / number theory pattern. Other problems with the same pattern: Bulb Switcher, Reaching Points, Water and Jug Problem.

Is there a brute force solution for Rectangle Overlap?

Yes. Rule out the four ways to be apart takes O(1) time and O(1) space. Two rectangles have no shared area exactly when one of them lies fully on one side of the other: to the left, to the right, below, or above.

Which edge cases should I test for Rectangle Overlap?

Rectangles that share only an edge (false); Rectangles that share only a corner (false); One rectangle completely inside the other (true); Identical rectangles (true); A cross shape: overlap in the middle even though no corner of either rectangle is inside the other (true); Overlap on the x-axis but a gap on the y-axis, or the other way round (false); Coordinates near plus or minus 10^9: the overlap's width times its height can reach 4 × 10^18, far past a 32-bit int, so test the two axes separately instead of computing an area.