Check if any two intervals overlap among a given set of intervals - GeeksQuiz
An interval is represented as a combination of start time and end time. Given a set of intervals, check if any two intervals overlap.
Read full article from Check if any two intervals overlap among a given set of intervals - GeeksQuiz
An interval is represented as a combination of start time and end time. Given a set of intervals, check if any two intervals overlap.
1. Find the overall maximum element. Let it be max_ele
2. Initialize an array of size max_ele with 0.
3. For every interval [start, end], increment the value at index start, i.e. arr[start]++ and decrement the value at index (end + 1), i.e. arr[end + 1]- -.
4. Compute the prefix sum of this array (arr[]).
5. Every index, i of this prefix sum array will tell how many times i has occurred in all the intervals taken together. If this value is greater than 1, then it occurs in 2 or more intervals.
6. So, simply initialize the result variable as false and while traversing the prefix sum array, change the result variable to true whenever the value at that index is greater than 1.
Time Complexity : O(max_ele + n)
Note: This method is more efficient than Method 1 if there are more number of intervals and at the same time maximum value among all intervals should be low, since time complexity is directly proportional to O(max_ele).
Note: This method is more efficient than Method 1 if there are more number of intervals and at the same time maximum value among all intervals should be low, since time complexity is directly proportional to O(max_ele).