Find if two rectangles overlap
Last Updated :
11 Sep, 2025
Given two rectangles, find if the given two rectangles overlap or not.
Note that a rectangle can be represented by two coordinates, top left and bottom right. So mainly we are given following four coordinates.
l1: Top Left coordinate of first rectangle.
r1: Bottom Right coordinate of first rectangle.
l2: Top Left coordinate of second rectangle.
r2: Bottom Right coordinate of second rectangle.

Examples:
Input: l1 = { 0, 10 }, r1 = { 10, 0 }, l2 = { 5, 5 }, r2 = { 15, 0 }
Output: Rectangles Overlap
Input: l1 = { 0, 10 }, r1 = { 10, 0 }, l2 = { -10, 5 }, r2 = { -1, 0 }
Output: Rectangles Don't Overlap
Note : It may be assumed that the rectangles are parallel to the coordinate axis.
One solution is to one by one pick all points of one rectangle and see if the point lies inside the other rectangle or not. This can be done using the algorithm discussed here.
Following is a simpler approach. Two rectangles do not overlap if one of the following conditions is true.
1) One rectangle is above top edge of other rectangle.
2) One rectangle is on left side of left edge of other rectangle.
We need to check above cases to find out if given rectangles overlap or not.
C++
// CPP program for the above approach
#include <bits/stdc++.h>
struct Point {
int x, y;
};
// Returns true if two rectangles (l1, r1) and (l2, r2)
// overlap
bool doOverlap(Point l1, Point r1, Point l2, Point r2)
{
if (l1.x > r2.x || l2.x > r1.x)
return false;
// If one rectangle is above the other
if (r1.y > l2.y || r2.y > l1.y)
return false;
return true;
}
/* Driver program to test above function */
int main()
{
Point l1 = { 0, 10 }, r1 = { 10, 0 };
Point l2 = { 5, 5 }, r2 = { 15, 0 };
if (doOverlap(l1, r1, l2, r2))
printf("Rectangles Overlap");
else
printf("Rectangles Don't Overlap");
return 0;
}
C
#include <stdio.h>
struct Point {
int x, y;
};
// Returns true if two rectangles (l1, r1) and (l2, r2)
// overlap
int doOverlap(struct Point l1, struct Point r1, struct Point l2, struct Point r2)
{
if (l1.x > r2.x || l2.x > r1.x)
return 0;
// If one rectangle is above the other
if (r1.y > l2.y || r2.y > l1.y)
return 0;
return 1;
}
/* Driver program to test above function */
int main()
{
struct Point l1 = { 0, 10 }, r1 = { 10, 0 };
struct Point l2 = { 5, 5 }, r2 = { 15, 0 };
if (doOverlap(l1, r1, l2, r2))
printf("Rectangles Overlap");
else
printf("Rectangles Don't Overlap");
return 0;
}
Java
public class RectangleOverlap {
static class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
// Returns true if two rectangles (l1, r1) and (l2, r2) overlap
static boolean doOverlap(Point l1, Point r1, Point l2, Point r2) {
// If one rectangle is to the left of the other
if (l1.x > r2.x || l2.x > r1.x)
return false;
// If one rectangle is above the other
if (r1.y > l2.y || r2.y > l1.y)
return false;
return true;
}
public static void main(String[] args) {
Point l1 = new Point(0, 10);
Point r1 = new Point(10, 0);
Point l2 = new Point(5, 5);
Point r2 = new Point(15, 0);
if (doOverlap(l1, r1, l2, r2))
System.out.println("Rectangles Overlap");
else
System.out.println("Rectangles Don't Overlap");
}
}
Python
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def do_overlap(l1, r1, l2, r2):
# If one rectangle is to the left of the other
if l1.x > r2.x or l2.x > r1.x:
return False
# If one rectangle is above the other
if r1.y > l2.y or r2.y > l1.y:
return False
return True
# Driver code
if __name__ == "__main__":
l1 = Point(0, 10)
r1 = Point(10, 0)
l2 = Point(5, 5)
r2 = Point(15, 0)
if do_overlap(l1, r1, l2, r2):
print("Rectangles Overlap")
else:
print("Rectangles Don't Overlap")
C#
using System;
class RectangleOverlap
{
public struct Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
// Returns true if two rectangles (l1, r1) and (l2, r2) overlap
static bool DoOverlap(Point l1, Point r1, Point l2, Point r2)
{
// If one rectangle is to the left of the other
if (l1.x > r2.x || l2.x > r1.x)
return false;
// If one rectangle is above the other
if (r1.y > l2.y || r2.y > l1.y)
return false;
return true;
}
static void Main()
{
Point l1 = new Point(0, 10);
Point r1 = new Point(10, 0);
Point l2 = new Point(5, 5);
Point r2 = new Point(15, 0);
if (DoOverlap(l1, r1, l2, r2))
Console.WriteLine("Rectangles Overlap");
else
Console.WriteLine("Rectangles Don't Overlap");
}
}
JavaScript
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
// Returns true if two rectangles (l1, r1) and (l2, r2) overlap
function doOverlap(l1, r1, l2, r2) {
// If one rectangle is to the left of the other
if (l1.x > r2.x || l2.x > r1.x)
return false;
// If one rectangle is above the other
if (r1.y > l2.y || r2.y > l1.y)
return false;
return true;
}
// Driver code
const l1 = new Point(0, 10);
const r1 = new Point(10, 0);
const l2 = new Point(5, 5);
const r2 = new Point(15, 0);
if (doOverlap(l1, r1, l2, r2)) {
console.log("Rectangles Overlap");
} else {
console.log("Rectangles Don't Overlap");
}
Python Program to Find if Two Rectangles Overlap
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem