![]() |
VOOZH | about |
An interval is represented as a combination of start time and end time. Given a set of intervals, we need to write a program to check if any interval completely overlaps the other.
Examples:
Input: arr[] = {{1, 3}, {1, 7}, {4, 8}, {2, 5}}
Output: true
Explanation: The intervals {1, 3} completely overlaps in {1, 7}.Input: arr[] = {{1, 3}, {7, 9}, {4, 6}, {10, 13}}
Output: false
Explanation: No pair of intervals overlap.
Naive Approach
A Simple Solution is to consider every pair of intervals and check if the pair overlaps or not.
Follow the given steps to solve the problem:
Below is the implementation of the approach:
Yes No
Time Complexity: O(n*n) as two nested for loops are executed. Here, n is size of the input array.
Auxiliary Space: O(1) as no extra space has been taken.
A better solution is to use Sorting. The idea is to sort all intervals in increasing order of start time and then in the sorted array, if the end time of an interval is not more than the end time of the previous interval, then there is a complete overlap.
Follow the given steps to solve the problem:
Given below is an implementation of the above approach:
Yes No
Time Complexity: O(n log n).
Auxiliary Space: O(1)