# Solution For Rectangle Overlap

Rectangle Overlap is a problem on LeetCode where we are given two rectangles in a plane, each defined by two sets of coordinates, (x1, y1), (x2, y2) representing the bottom-left and top-right corners respectively. Our task is to determine if the two rectangles overlap or not.

To solve this problem, we need to first understand what it means for two rectangles to overlap. Two rectangles overlap if and only if their x and y ranges intersect. The x range for a rectangle is (x1, x2) while the y range is (y1, y2).

Thus, to determine if the two rectangles overlap, we need to check if their x and y ranges intersect.

The first step is to find the x and y ranges of the two rectangles. We can do this by computing the minimum and maximum values for x and y for both rectangles.

Code to find x and y ranges for each rectangle:

“`
int x1 = Math.min(rec1[0], rec1[2]);
int y1 = Math.min(rec1[1], rec1[3]);
int x2 = Math.max(rec1[0], rec1[2]);
int y2 = Math.max(rec1[1], rec1[3]);

int x3 = Math.min(rec2[0], rec2[2]);
int y3 = Math.min(rec2[1], rec2[3]);
int x4 = Math.max(rec2[0], rec2[2]);
int y4 = Math.max(rec2[1], rec2[3]);
“`

Next, we need to check if the x and y ranges intersect. If they do, the rectangles overlap, and we return true. Otherwise, they do not overlap and we return false.

Code to check for intersection:

`if (x1 >= x4 || x2 <= x3 || y1 >= y4 || y2 <= y3) { return false; } else { return true; }`

The condition to check for intersection is that one rectangle’s x range is entirely to the left of the other rectangle’s x range (x1 >= x4 or x2 <= x3) or one rectangle’s y range is entirely above or below the other rectangle’s y range (y1 >= y4 or y2 <= y3). In either case, the rectangles do not intersect.

The complete code for the Rectangle Overlap problem is as follows:

“`
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
int x1 = Math.min(rec1[0], rec1[2]);
int y1 = Math.min(rec1[1], rec1[3]);
int x2 = Math.max(rec1[0], rec1[2]);
int y2 = Math.max(rec1[1], rec1[3]);

``````int x3 = Math.min(rec2[0], rec2[2]);
int y3 = Math.min(rec2[1], rec2[3]);
int x4 = Math.max(rec2[0], rec2[2]);
int y4 = Math.max(rec2[1], rec2[3]);

if (x1 >= x4 || x2 <= x3 || y1 >= y4 || y2 <= y3) {
return false;
} else {
return true;
}
``````

}
“`

This solution has a time complexity of O(1), as we are performing a constant number of operations on the input coordinates.

## Step by Step Implementation For Rectangle Overlap

```/**
* @author:
* @date:
* @description:
*/

public class Solution {
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {

}
}```
```def isRectangleOverlap(rec1, rec2):

# find the width of the overlap on the x axis
# find the height of the overlap on the y axis

# if the width and height are both positive, then there is an overlap
# otherwise, there is no overlap

# rec1 and rec2 are lists of four integers: x1, y1, x2, y2

# find the width of the overlap on the x axis
x_overlap = min(rec1[2], rec2[2]) - max(rec1[0], rec2[0])

# find the height of the overlap on the y axis
y_overlap = min(rec1[3], rec2[3]) - max(rec1[1], rec2[1])

# if the width and height are both positive, then there is an overlap
# otherwise, there is no overlap
if x_overlap > 0 and y_overlap > 0:
return True
else:
return False```
```/**
* @param {number} rec1_x
* @param {number} rec1_y
* @param {number} rec1_w
* @param {number} rec1_h
* @param {number} rec2_x
* @param {number} rec2_y
* @param {number} rec2_w
* @param {number} rec2_h
* @returns {boolean}
*/
const rectOverlap = (
rec1_x,
rec1_y,
rec1_w,
rec1_h,
rec2_x,
rec2_y,
rec2_w,
rec2_h
) => {
// if one rectangle is on left side of other
if (rec1_x > rec2_x + rec2_w || rec2_x > rec1_x + rec1_w) {
return false;
}

// if one rectangle is above other
if (rec1_y > rec2_y + rec2_h || rec2_y > rec1_y + rec1_h) {
return false;
}

return true;
};```
```There are many possible solutions to this problem. One approach would be to use a brute force approach, where we iterate through every possible pair of rectangles and check if they overlap. However, this approach is not very efficient, as it has a time complexity of O(n^2), where n is the number of rectangles.

A more efficient solution would be to use a sweep line algorithm. We can sort the rectangles by their x-coordinates, and then iterate through them. For each rectangle, we check if there are any other rectangles that overlap with it. If there are, we add them to a list of overlapping rectangles. We can then repeat this process for the y-coordinates.

The time complexity of this approach is O(n log n), which is much better than the brute force approach.```
```public class Solution {
public bool RectangleOverlap(int[] rec1, int[] rec2) {
//check if the two rectangles overlap
//overlap if rec1's left edge is to the right of rec2's right edge
//AND rec1's right edge is to the left of rec2's left edge
//AND rec1's top edge is below rec2's bottom edge
//AND rec1's bottom edge is above rec2's top edge
return !(rec1[2] <= rec2[0] ||
rec1[3] <= rec2[1] ||
rec1[0] >= rec2[2] ||
rec1[1] >= rec2[3]);
}
}```
Scroll to Top

## Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]