




Sponsored
Sponsored
This approach checks if there are conditions that confirm non-overlapping rectangles. Specifically, two rectangles do not overlap if:
If any of these conditions are true, the rectangles do not overlap. Otherwise, they do overlap.
Time Complexity: O(1), since the operation involves a constant number of comparisons.
Space Complexity: O(1), since the space used by the function is constant with respect to inputs.
1#include <stdbool.h>
2bool isRectangleOverlap(int* rec1, int* rec2) {
3    return !(rec1[2] <= rec2[0] ||
4             rec1[0] >= rec2[2] ||
5             rec1[3] <= rec2[1] ||
6             rec1[1] >= rec2[3]);
7}In C, checking if rectangles overlap relies on the same set of boolean expressions used above in other languages.
This approach computes the potential intersecting rectangle and verifies if its calculated dimensions form a valid rectangle with a positive area. The logic involves:
max(rec1[0], rec2[0])min(rec1[2], rec2[2])max(rec1[1], rec2[1])min(rec1[3], rec2[3])Time Complexity: O(1)
Space Complexity: O(1)
The function calculates potential overlapping parts along both axes and ensures these parts form an area (both x and y overlap must be true).