Check if any two intervals overlap among a given set of intervals

An interval is represented as a combination of start time and end time. Given a set of intervals, check if any two intervals overlap.

Input:  arr[] = {{1, 3}, {5, 7}, {2, 4}, {6, 8}}
Output: true
The intervals {1, 3} and {2, 4} overlap


Input:  arr[] = {{1, 3}, {7, 9}, {4, 6}, {10, 13}}
Output: false
No pair of intervals overlap. 

Expected time complexity is O(nLogn) where n is number of intervals.

We strongly recommend to minimize your browser and try this yourself first.



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(n2)

Method 1
A better solution is to Use Sorting. Following is complete algorithm.
1) Sort all intervals in increasing order of start time. This step takes O(nLogn) time.
2) In the sorted array, if start time of an interval is less than end of previous interval, then there is an overlap. This step takes O(n) time.

So overall time complexity of the algorithm is O(nLogn) + O(n) which is O(nLogn).

Below is C++ implementation of above idea.

filter_none

edit
close

play_arrow

link
brightness_4
code

// A C++ program to check if any two intervals overlap
#include <algorithm>
#include <iostream>
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(). See http:// goo.gl/iGspV
bool compareInterval(Interval i1, Interval i2)
{
    return (i1.start < i2.start) ? true : false;
}
  
// Function to check if any two intervals overlap
bool isOverlap(Interval arr[], int n)
{
    // Sort intervals in increasing order of start time
    sort(arr, arr + n , compareInterval);
  
    // In the sorted array, if start time of an interval
    // is less than end of previous interval, then there
    // is an overlap
    for (int i = 1; i < n; i++)
        if (arr[i - 1].end > arr[i].start)
            return true;
  
    // If we reach here, then no overlap
    return false;
}
  
// Driver program
int main()
{
    Interval arr1[] = { { 1, 3 }, { 7, 9 }, { 4, 6 }, { 10, 13 } };
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    isOverlap(arr1, n1) ? cout << "Yes\n" : cout << "No\n";
  
    Interval arr2[] = { { 6, 8 }, { 1, 3 }, { 2, 4 }, { 4, 7 } };
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    isOverlap(arr2, n2) ? cout << "Yes\n" : cout << "No\n";
  
    return 0;
}

chevron_right


Output:

No
Yes

Method 2: This approach is suggested by Anjali Agarwal. Following are the steps:

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.

Below is the implementation of this (Method 2) approach.

filter_none

edit
close

play_arrow

link
brightness_4
code

// A C++ program to check if any two intervals overlap
#include <algorithm>
#include <iostream>
using namespace std;
  
// An interval has start time and end time
struct Interval {
    int start;
    int end;
};
  
// Function to check if any two intervals overlap
bool isOverlap(Interval arr[], int n)
{
  
    int max_ele = 0;
  
    // Find the overall maximum element
    for (int i = 0; i < n; i++) {
        if (max_ele < arr[i].end)
            max_ele = arr[i].end;
    }
  
    // Initialize an array of size max_ele
    int aux[max_ele + 1] = { 0 };
    for (int i = 0; i < n; i++) {
  
        // starting point of the interval
        int x = arr[i].start;
  
        // end point of the interval
        int y = arr[i].end;
        aux[x]++, aux[y + 1]--;
    }
    for (int i = 1; i <= max_ele; i++) {
        // Calculating the prefix Sum
        aux[i] += aux[i - 1];
  
        // Overlap
        if (aux[i] > 1)
            return true;
    }
  
    // If we reach here, then no Overlap
    return false;
}
  
// Driver program
int main()
{
    Interval arr1[] = { { 1, 3 }, { 7, 9 }, { 4, 6 }, { 10, 13 } };
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
  
    isOverlap(arr1, n1) ? cout << "Yes\n" : cout << "No\n";
  
    Interval arr2[] = { { 6, 8 }, { 1, 3 }, { 2, 4 }, { 4, 7 } };
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    isOverlap(arr2, n2) ? cout << "Yes\n" : cout << "No\n";
  
    return 0;
}
// This Code is written by Anjali Agarwal

chevron_right


Output:

No
Yes

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).
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up

Improved By : smitm1, Akanksha_Rai



Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.