I tried the approach to first detect the conditions of non overlapping .If it holds then print 0 else print 1.
According to the approach,i am not able to pass first test case.
Here is my soln.
https://ide.codingblocks.com/s/360897
Ples chk out.
Test cases not getting passed completely
hello @rohangupta569
why so many conditions?
If the rectangles do not overlap, then rec1 must either be higher, lower, to the left, or to the right of rec2.
So the conditions that the two rectangles will not overlap will be as follows:-
If rec1 left of rec2:
rec1[2] <= rec2[0], i.e rightmost x coordinate of rec1 is smaller than or equal to the leftmost x coordinate of rec2.
Or
If rec1 is right of rec2:
rec1[0] >= rec2[2], i.e leftmost x coordinate of rec1 is greater than or equal to the rightmost x coordinate of rec2.
Or
If rec1 is above of rec2:
rec1[1] >= rec2[3], i.e leftmost y coordinate of rec1 is greater than or equal to rightmost y coordinate of rec2.
Or
If rec1 is below of rec2:
rec1[3] <= rec2[1], i.e rightmost y coordinate of rec1 is lesser than or equal to leftmost y coordinate of rec2.
The negation of these 4 conditions will be the condition for rectangles to overlap i.e !(cond1 || cond2 || cond3 || cond4).
#include <iostream>
using namespace std;
bool isOverLap(int rec1[], int rec2[]) {
return !(rec1[2] <= rec2[0] ||
rec1[3] <= rec2[1] ||
rec1[0] >= rec2[2] ||
rec1[1] >= rec2[3]);
}
int main() {
int rec1[4];
int rec2[4];
for(int i = 0; i < 4; i++) {
cin >> rec1[i];
}
for(int i = 0; i < 4; i++) {
cin >> rec2[i];
}
cout << isOverLap(rec1, rec2);
}
that is what i did i put 4 conditions maxx1 denotes the maximum x coordinate among x1 and x2 for rectangle 1.And chk function will return true if they donot overlap so why my code is giving wrong answer in Test case1.
check for this->
0 10 10 0
5 5 15 0
answer should be 0
But it is an overlapping case…Rectangle is overlapping from (5,5) to (10,0) in this case then why its answer is 0
yeah the test cases is wrong.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.