Find if Two Rectangles Overlap

Last Updated : 3 Oct, 2026

Given two rectangles, find if the given two rectangles overlap or not.

  • A rectangle is denoted by providing the x and y coordinates of two points: the left top corner and the right bottom corner of the rectangle.
  • Two rectangles sharing a side are considered overlapping. (l1[] and r1[] are the extreme points of the first rectangle and l2[] and r2[] are the extreme points of the second rectangle).

Note: It may be assumed that the rectangles are parallel to the coordinate axis.

rectanglesOverlap

Examples:

Input: l1[] = [0, 10], r1[] = [10, 0], l2[] = [5, 5], r2[] = [15, 0]
Output: true
Explanation: The rectangles overlap.

Input: l1[] = [0, 2], r1[] = [1, 1], l2[] = [-2, 0], r2[] = [0, -3]
Output: false
Explanation: The rectangles do not overlap.

Try It Yourself
redirect icon

[Expected Approach] Axis-Aligned Bounding Box (AABB) Overlap Check - O(1) Time and O(1) Space

The idea is to check whether two rectangles overlap by checking the conditions under which they do not overlap, and then inverting the result.

Two axis-aligned rectangles fail to overlap if and only if one is completely to the left, right, above, or below the other.

  • Horizontal Separation: Rectangle 1 is completely to the left of Rectangle 2 if its right boundary is to the left of Rectangle 2's left boundary (l1[0] > r2[0]), and vice versa (l2[0] > r1[0]).
  • Vertical Separation: Rectangle 1 is completely below Rectangle 2 if its top boundary is lower than Rectangle 2's bottom boundary (l1[1] < r2[1]), and vice versa (l2[1] < r1[1]).

If none of these separation conditions are met, the rectangles must overlap, and the function returns true.

C++
#include 
using namespace std;

bool doOverlap(vector<int> &l1, vector<int> &r1, vector<int> &l2, vector<int> &r2)
{

    // If one rectangle is on left side of other
    if (l1[0] > r2[0] || l2[0] > r1[0])
        return false;

    // If one rectangle is above other
    if (l1[1] < r2[1] || l2[1] < r1[1])
        return false;

    return true;
}

int main()
{

    // Test Case 1: Overlapping rectangles
    vector<int> l1 = {0, 10}, r1 = {10, 0};
    vector<int> l2 = {5, 5}, r2 = {15, 0};
    cout << (doOverlap(l1, r1, l2, r2) ? "true" : "false") << "\n";

    // Test Case 2: Non-overlapping rectangles
    vector<int> l3 = {0, 2}, r3 = {1, 1};
    vector<int> l4 = {-2, 0}, r4 = {0, -3};
    cout << (doOverlap(l3, r3, l4, r4) ? "true" : "false") << "\n";

    return 0;
}
Java
class GFG {
    static boolean doOverlap(int[] l1, int[] r1, int[] l2,
                             int[] r2)
    {

        // If one rectangle is on left side of other
        if (l1[0] > r2[0] || l2[0] > r1[0])
            return false;

        // If one rectangle is above other
        if (l1[1] < r2[1] || l2[1] < r1[1])
            return false;

        return true;
    }

    public static void main(String[] args)
    {

        // Test Case 1: Overlapping rectangles
        int[] l1 = { 0, 10 }, r1 = { 10, 0 };
        int[] l2 = { 5, 5 }, r2 = { 15, 0 };
        System.out.println(doOverlap(l1, r1, l2, r2));

        // Test Case 2: Non-overlapping rectangles
        int[] l3 = { 0, 2 }, r3 = { 1, 1 };
        int[] l4 = { -2, 0 }, r4 = { 0, -3 };
        System.out.println(doOverlap(l3, r3, l4, r4));
    }
}
Python
def doOverlap(l1, r1, l2, r2):

    # If one rectangle is on left side of other
    if l1[0] > r2[0] or l2[0] > r1[0]:
        return False

    # If one rectangle is above other
    if l1[1] < r2[1] or l2[1] < r1[1]:
        return False

    return True


if __name__ == "__main__":

    # Test Case 1: Overlapping rectangles
    l1, r1, l2, r2 = [0, 10], [10, 0], [5, 5], [15, 0]
    print(str(doOverlap(l1, r1, l2, r2)).lower())

    # Test Case 2: Non-overlapping rectangles
    l3, r3, l4, r4 = [0, 2], [1, 1], [-2, 0], [0, -3]
    print(str(doOverlap(l3, r3, l4, r4)).lower())
C#
using System;

class GFG {
    public static bool doOverlap(int[] l1, int[] r1,
                                 int[] l2, int[] r2)
    {

        // If one rectangle is on left side of other
        if (l1[0] > r2[0] || l2[0] > r1[0])
            return false;

        // If one rectangle is above other
        if (l1[1] < r2[1] || l2[1] < r1[1])
            return false;

        return true;
    }

    static void Main(string[] args)
    {

        // Test Case 1: Overlapping rectangles
        int[] l1 = { 0, 10 }, r1 = { 10, 0 };
        int[] l2 = { 5, 5 }, r2 = { 15, 0 };
        Console.WriteLine(
            doOverlap(l1, r1, l2, r2) ? "true" : "false");

        // Test Case 2: Non-overlapping rectangles
        int[] l3 = { 0, 2 }, r3 = { 1, 1 };
        int[] l4 = { -2, 0 }, r4 = { 0, -3 };
        Console.WriteLine(
            doOverlap(l3, r3, l4, r4) ? "true" : "false");
    }
}
JavaScript
function doOverlap(l1, r1, l2, r2)
{

    // If one rectangle is on left side of other
    if (l1[0] > r2[0] || l2[0] > r1[0]) {
        return false;
    }

    // If one rectangle is above other
    if (l1[1] < r2[1] || l2[1] < r1[1]) {
        return false;
    }

    return true;
}

// Driver Code

// Test Case 1: Overlapping rectangles
let l1 = [ 0, 10 ], r1 = [ 10, 0 ];
let l2 = [ 5, 5 ], r2 = [ 15, 0 ];
console.log(doOverlap(l1, r1, l2, r2) ? "true" : "false");

// Test Case 2: Non-overlapping rectangles
let l3 = [ 0, 2 ], r3 = [ 1, 1 ];
let l4 = [ -2, 0 ], r4 = [ 0, -3 ];
console.log(doOverlap(l3, r3, l4, r4) ? "true" : "false");

Output
true
false
Comment