Overlapping rectangles | Practice | GeeksforGeeks

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.
Example 1:
Input:
L1=(0,10)
R1=(10,0)
L2=(5,5)
R2=(15,0)
Output:
1
Explanation:
The rectangles overlap.
Example 2:
Input:
L1=(0,2)
R1=(1,1)
L2=(-2,0)
R2=(0,-3)
Output:
0
Explanation:
The rectangles do not overlap.

Your Task:
You don't need to read input or print anything. Your task is to complete the function doOverlap() which takes the points L1, R1, L2, and R2 as input parameters and returns 1 if the rectangles overlap. Otherwise, it returns 0.

Expected Time Complexity:O(1)
Expected Auxillary Space:O(1)

Constraints:
-109<=x-coordinate,y-coordinate<=109
0
billi1 week ago
 int doOverlap(int l1[], int r1[], int l2[], int r2[]) {
        int a1=l1[0];
        int b1=l1[1];
        int a2=r1[0];
        int b2=r1[1];
        
        int x1=l2[0];
        int y1=l2[1];
        int x2=r2[0];
        int y2=r2[1];
        //// there four posssible cndition in which 2 rectange does not overlap ...
        //// 1st one when lower index y of 2nd rectangle(y2)> upper index y of 1st
        //->y2>b1;
        ///if this the case then it must have its vice versa where ....
        ////b2>y1
        
        /// nxt we do the comparison of x coordinate.....
        /// if a1>x2 then not valid 
        /// and similar fashion if x1>a2 then not valid....
        
        /// and in rest cases rectangle will overlapp....
        
        
        if(b1<y2||a2<x1||x2<a1||y1<b2)return 0;
        return 1;
    }
+2
sandeepkmrnayak2 weeks ago
Simple C++ Solution:
int doOverlap(int L1[], int R1[], int L2[], int R2[]) {       // code here       if(L1[1] < R2[1] || R1[0] < L2[0] || L1[0] > R2[0] || R1[1] > L2[1]){           return 0;       }       return 1;   }
0
tharabhai4 months ago
C++ inplementation:
 
int doOverlap(int L1[], int R1[], int L2[], int R2[]) {       // Calculate midpoints of both rectangles then check the distance between them.
       float mid1[2],mid2[2];              mid1[0] = (R1[0]-L1[0])/2.0;       mid1[1] = (L1[1]-R1[1])/2.0;              mid2[0] = (R2[0]-L2[0])/2.0;       mid2[1] = (L2[1]-R2[1])/2.0;              float max_x = mid1[0] + mid2[0];       float max_y = mid1[1] + mid2[1];              mid1[0] = L1[0] + ((R1[0]-L1[0])/2.0);       mid1[1] = R1[1] + ((L1[1]-R1[1])/2.0);              mid2[0] = L2[0] + ((R2[0]-L2[0])/2.0);       mid2[1] = R2[1] + ((L2[1]-R2[1])/2.0);                  if(abs(mid2[0]-mid1[0])>max_x || abs(mid2[1]-mid1[1])>max_y){           return 0;       }       return 1;   }
0
chechipresh4 months ago
What about a case like 
L1(1,6) R1(2,4) L2(2,4) R2(5,2)   (one of thecorner points of two rectangles coinciding)
Should it be considered overlapping or non overlapping?
0
noviicee6 months ago
I don't know why, but checking if the rectangle is actually is a line, leads to failure of some test-cases ?
+6
jainisha5306 months ago
class Solution { public:   int doOverlap(int L1[], int R1[], int L2[], int R2[]) {       // code here       if(L2[0]>R1[0]||L1[0]>R2[0]||L1[1]<R2[1]||L2[1]<R1[1])       return 0;       return 1;   }};
0
shahsourabh116 months ago
//c++ //
class Solution { public:   int doOverlap(int L1[], int R1[], int L2[], int R2[]) {       // code here       return min(R1[0],R2[0])>=max(L1[0],L2[0]) && min(L1[1],L2[1])>=max(R1[1],R2[1]);   }}; 
+7
riyajha23056 months ago
//C++ solution
 
class Solution { public:   int 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 (r1[1] > l2[1] || r2[1] > l1[1])           return false;   return true;   }};
+3
dishasarna20016 months ago
Expected Time Complexity:O(1)Expected Auxillary Space:O(1)
//C++ Solution
class Solution { public:   int doOverlap(int L1[], int R1[], int L2[], int R2[]) {       // code here       //If one rectangle is on left side of the left edge of the other rectangle       if(L1[0]>R2[0] || L2[0]>R1[0])           return false;       //If one rectangle is above the other       if(L1[1]<R2[1] || L2[1]<R1[1])           return false;       return true;   }};
 
 
+2
mridulkapoor1234566 months ago
Expected Time Complexity:O(1)Expected Auxillary Space:O(1)
class Solution {
  public:
    int doOverlap(int L1[], int R1[], int L2[], int R2[]) {
        // code here
        //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 the other
        if(L1[1]<R2[1] || L2[1]<R1[1])
            return false;
        return true;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.