Given a set of intervals, print all non-overlapping intervals after merging the overlapping intervals.

For example,

Input:  {1, 5}, {2, 3}, {4, 6}, {7, 8}, {8, 10}, {12, 15}
 
Output: Intervals after merging overlapping intervals are {1, 6}, {7, 10}, {12, 15}.

Practice this problem

The idea is to sort the intervals in increasing order of their starting time. Then create an empty stack and for each interval,

  • If the stack is empty or the top interval in the stack does not overlap with the current interval, push it into the stack.
  • If the top interval of the stack overlaps with the current interval, merge both intervals by updating the end of the top interval at the ending of the current interval.

Finally, print all non-overlapping intervals present in the stack. The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

{12, 15}
{7, 10}
{1, 6}

Java


Download  Run Code

Output:

{12, 15}
{7, 10}
{1, 6}

Python


Download  Run Code

Output:

(12, 15)
(7, 10)
(1, 6)

The time complexity of the above solution O(n.log(n)) and requires O(n) extra space, where n is the total number of given intervals.