Find Non-overlapping intervals among a given set of intervals

Given N set of time intervals, the task is to find the intervals which don’t overlap with the given set of intervals.

Examples:

Input: interval arr[] = { {1, 3}, {2, 4}, {3, 5}, {7, 9} }
Output:
[5, 7]
Explanation:
The only interval which doesn’t overlaps with the other intervals is [5, 7].

Input: interval arr[] = { {1, 3}, {9, 12}, {2, 4}, {6, 8} }
Output:
[4, 6]
[8, 9]
Explanation:
There are two intervals which don’t overlap with other intervals are [4, 6], [8, 9].

Approach: The idea is to sort the given time intervals according to starting time and if the consecutive intervals don’t overlap then the difference between them is the free interval.
Below are the steps:



  1. Sort the given set of intervals according to starting time.
  2. Traverse all the set of intervals and check whether the consecutive intervals overlaps or not.
  3. If the intervals(say interval a & interval b) doesn’t overlap then the set of pairs form by [a.end, b.start] is the non-overlapping interval.
  4. If the intervals overlaps, then check for next consecutive intervals.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include "bits/stdc++.h"
using namespace std;
  
// interval with start time & end time
struct interval {
    int start, end;
};
  
// Comparator function to sort the given
// interval according to time
bool compareinterval(interval i1, interval i2)
{
    return (i1.start < i2.start);
}
  
// Function that find the free interval
void findFreeinterval(interval arr[], int N)
{
  
    // If there are no set of interval
    if (N <= 0) {
        return;
    }
  
    // To store the set of free interval
    vector<pair<int, int> > P;
  
    // Sort the given interval according
    // starting time
    sort(arr, arr + N, compareinterval);
  
    // Iterate over all the interval
    for (int i = 1; i < N; i++) {
  
        // Previous interval end
        int prevEnd = arr[i - 1].end;
  
        // Current interval start
        int currStart = arr[i].start;
  
        // If ending index of previous
        // is less than starting index
        // of current, then it is free
        // interval
        if (prevEnd < currStart) {
            P.push_back({ prevEnd,
                          currStart });
        }
    }
  
    // Print the free interval
    for (auto& it : P) {
        cout << "[" << it.first << ", "
             << it.second << "]" << endl;
    }
}
  
// Driver Code
int main()
{
  
    // Given set of interval
    interval arr[] = { { 1, 3 },
                       { 2, 4 },
                       { 3, 5 },
                       { 7, 9 } };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    findFreeinterval(arr, N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
class GFG {
      
// Interval with start time & end time
static class Interval
{
    int start, end;
  
    Interval(int start, int end)
    {
        this.start = start;
        this.end = end;
    }
}
  
// Function that find the free interval
static void findFreeinterval(int[][] arr, int N)
{
    // If there are no set of interval
    if (N <= 0
    {
        return;
    }
  
    // To store the set of free interval
    ArrayList<Interval> p = new ArrayList<>();
  
    // Sort the given interval according
    // starting time
    Arrays.sort(arr, new Comparator<int[]>() 
    {
        public int compare(int[] a, int[] b)
        {
            return a[0] - b[0];
        }
    });
  
    // Iterate over all the interval
    for (int i = 1; i < N; i++) 
    {
  
        // Previous interval end
        int prevEnd = arr[i - 1][1];
  
        // Current interval start
        int currStart = arr[i][0];
  
        // If ending index of previous
        // is less than starting index
        // of current, then it is free
        // interval
        if (prevEnd < currStart) 
        {
            Interval interval = new Interval(prevEnd,
                                              currStart);
            p.add(interval);
        }
    }
  
    // Print the free interval
    for (int i = 0; i < p.size(); i++)
    {
        System.out.println("[" + p.get(i).start + 
                          ", " + p.get(i).end + "]");
    }
}
  
// Driver code
public static void main(String[] args)
{
  
    // Given set of interval
    int[][] arr = { { 1, 3 },
                    { 2, 4 },
                    { 3, 5 },
                    { 7, 9 } };
  
    int N = arr.length;
  
    // Function Call
    findFreeinterval(arr, N);
}
}
  
// This code is contributed by offbeat

chevron_right


Output:

[5, 7]

Time Complexity: O(N*log N), where N is the number of set of intervals.

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : offbeat

Article Tags :
Practice Tags :


Be the First to upvote.


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