Given two closed intervals [A1, B1] and [A2, B2], check and return if the two intervals intersect each other.
A closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b.
Two closed intervals are said to intersect if they have common numbers, i.e. that appear in both intervals.
Example 1:
Input: A1 = 1, B1 = 6, A2 = 3, B2 = 8 Output: true
Example 2:
Input: A1 = 1, B1 = 4, A2 = 4, B2 = 6 Output: true
Example 3:
Input: A1 = 1, B1 = 8, A2 = 4, B2 = 6 Output: true
Example 4:
Input: A1 = 4, B1 = 6, A2 = 1, B2 = 3 Output: false
Note:
Your algorithm should run in O(1) time and use O(1) extra space.
Hints:
Instead of thinking about when the intervals intersect each other, it's easier to think about when they do not intersect each other.
The intervals do not intersect each other when one of them is "to the left" of the other one i.e.
B1 < A2 or B2 < A1.
We can write an
if statement checking if B1 < A2 or B2 < A1. If so, we return false. Otherwise, we return true.
You can implement this using no
else statements. You can even implement it in one line using only one return.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check