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 The intervals {1, 3} completely overlaps in {1, 7}. Input: arr[] = {{1, 3}, {7, 9}, {4, 6}, {10, 13}} Output: false No pair of intervals overlap.

A **Simple Solution** is to consider every pair of intervals and check if the pair overlaps or not. The time complexity of this solution is O(n^{2}).

A **better solution** is to use **Sorting**. Following is the complete algorithm.

1) Sort all intervals in increasing order of start time. This step takes **O(n Logn)** time.

2) In the sorted array, if the end time of an interval is not more than the end of the previous interval, then there is a complete overlap. This step takes **O(n)** time.

Given below is an implementation of the above approach:

`// A C++ program to check if any two intervals ` `// completely overlap ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// An interval has start time and end time ` `struct` `Interval { ` ` ` `int` `start; ` ` ` `int` `end; ` `}; ` ` ` `// Compares two intervals according to their staring ` `// time. This is needed for sorting the intervals ` `// using library function std::sort(). ` `bool` `compareInterval(Interval i1, Interval i2) ` `{ ` ` ` `return` `(i1.start < i2.start) ? ` `true` `: ` `false` `; ` `} ` ` ` `// Function to check if any two intervals ` `// completely overlap ` `bool` `isOverlap(Interval arr[], ` `int` `n) ` `{ ` ` ` `// Sort intervals in increasing order of ` ` ` `// start time ` ` ` `sort(arr, arr + n - 1, compareInterval); ` ` ` ` ` `// In the sorted array, if end time of an ` ` ` `// interval is not more than that of ` ` ` `// end of previous interval, then there ` ` ` `// is an overlap ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `if` `(arr[i].end <= arr[i - 1].end) ` ` ` `return` `true` `; ` ` ` ` ` `// If we reach here, then no overlap ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// 1st example ` ` ` `Interval arr1[] = { { 1, 3 }, { 1, 7 }, { 4, 8 }, ` ` ` `{ 2, 5 } }; ` ` ` `int` `n1 = ` `sizeof` `(arr1) / ` `sizeof` `(arr1[0]); ` ` ` `if` `(isOverlap(arr1, n1)) ` ` ` `cout << ` `"Yes\n"` `; ` ` ` `else` ` ` `cout << ` `"No\n"` `; ` ` ` ` ` `// 2nd example ` ` ` `Interval arr2[] = { { 1, 3 }, { 7, 9 }, { 4, 6 }, ` ` ` `{ 10, 13 } }; ` ` ` `int` `n2 = ` `sizeof` `(arr2) / ` `sizeof` `(arr2[0]); ` ` ` `if` `(isOverlap(arr2, n2)) ` ` ` `cout << ` `"Yes\n"` `; ` ` ` `else` ` ` `cout << ` `"No\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Yes No

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Find the largest interval that contains exactly one of the given N integers.
- Count of available non-overlapping intervals to be inserted to make interval [0, R]
- Find the interval which contains maximum number of concurrent meetings
- Check if given string can be formed by two other strings or their permutations
- Check if any two intervals overlap among a given set of intervals
- Smallest subset with sum greater than all other elements
- Pairs such that one is a power multiple of other
- Get maximum items when other items of total cost of an item are free
- Longest subsequence with first and last element greater than all other elements
- Find the longest string that can be made up of other strings from the array
- Maximum possible remainder when an element is divided by other element in the array
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Find minimum difference between any two elements
- Python | Sort Tuples in Increasing Order by any key
- Program for sorting variables of any data type
- Maximum sum of absolute difference of any permutation
- Largest even number that can be formed by any number of swaps
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.