Given a set of **N** intervals, the task is to find the maximal set of mutually disjoint intervals. Two intervals **[i, j]** & **[k, l]** are said to be disjoint if they do not have any point in common.

**Examples:**

Input:intervals[][] = {{1, 4}, {2, 3}, {4, 6}, {8, 9}}

Output:

[2, 3]

[4, 6]

[8, 9]

Intervals sorted w.r.t. end points = {{2, 3}, {1, 4}, {4, 6}, {8, 9}}

Intervals [2, 3] and [1, 4] overlap.

We must include [2, 3] because if [1, 4] is included then

we cannot include [4, 6].

Input:intervals[][] = {{1, 9}, {2, 3}, {5, 7}}

Output:

[2, 3]

[5, 7]

**Approach:**

- Sort the intervals, with respect to their end points.
- Now, traverse through all the intervals, if we get two overlapping intervals, then greedily choose the interval with lower end point since, choosing it will ensure that intervals further can be accommodated without any overlap.
- Apply the same procedure for all the intervals and print all the intervals which satisfy the above criteria.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long int ` ` ` `// Function to sort the vector elements ` `// by second element of pairs ` `bool` `sortbysec(` `const` `pair<` `int` `, ` `int` `>& a, ` ` ` `const` `pair<` `int` `, ` `int` `>& b) ` `{ ` ` ` `return` `(a.second < b.second); ` `} ` ` ` `// Function to find maximal disjoint set ` `void` `maxDisjointIntervals(vector<pair<` `int` `, ` `int` `> > list) ` `{ ` ` ` ` ` `// Sort the list of intervals ` ` ` `sort(list.begin(), list.end(), sortbysec); ` ` ` ` ` `// First Interval will always be ` ` ` `// included in set ` ` ` `cout << ` `"["` `<< list[0].first << ` `", "` ` ` `<< list[0].second << ` `"]"` `<< endl; ` ` ` ` ` `// End point of first interval ` ` ` `int` `r1 = list[0].second; ` ` ` ` ` `for` `(` `int` `i = 1; i < list.size(); i++) { ` ` ` `int` `l1 = list[i].first; ` ` ` `int` `r2 = list[i].second; ` ` ` ` ` `// Check if given interval overlap with ` ` ` `// previously included interval, if not ` ` ` `// then include this interval and update ` ` ` `// the end point of last added interval ` ` ` `if` `(l1 > r1) { ` ` ` `cout << ` `"["` `<< l1 << ` `", "` ` ` `<< r2 << ` `"]"` `<< endl; ` ` ` `r1 = r2; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` `vector<pair<` `int` `, ` `int` `> > intervals = { { 1, 4 }, ` ` ` `{ 2, 3 }, ` ` ` `{ 4, 6 }, ` ` ` `{ 8, 9 } }; ` ` ` `maxDisjointIntervals(intervals); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to find maximal disjoint set ` `def` `maxDisjointIntervals(list_): ` ` ` ` ` `# Lambda function to sort the list ` ` ` `# elements by second element of pairs ` ` ` `list_.sort(key ` `=` `lambda` `x: x[` `1` `]) ` ` ` ` ` `# First interval will always be ` ` ` `# included in set ` ` ` `print` `(` `"["` `, list_[` `0` `][` `0` `], ` `", "` `, list_[` `0` `][` `1` `], ` `"]"` `) ` ` ` ` ` `# End point of first interval ` ` ` `r1 ` `=` `list_[` `0` `][` `1` `] ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(list_)): ` ` ` `l1 ` `=` `list_[i][` `0` `] ` ` ` `r2 ` `=` `list_[i][` `1` `] ` ` ` ` ` `# Check if given interval overlap with ` ` ` `# previously included interval, if not ` ` ` `# then include this interval and update ` ` ` `# the end point of last added interval ` ` ` `if` `l1 > r1: ` ` ` `print` `(` `"["` `, l1, ` `", "` `, r2, ` `"]"` `) ` ` ` `r1 ` `=` `r2 ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `4` ` ` `intervals ` `=` `[ [ ` `1` `, ` `4` `], [ ` `2` `, ` `3` `], ` ` ` `[ ` `4` `, ` `6` `], [ ` `8` `, ` `9` `] ] ` ` ` ` ` `# Call the function ` ` ` `maxDisjointIntervals(intervals) ` ` ` `# This code is contributed by Tokir Manva ` |

*chevron_right*

*filter_none*

**Output:**

[2, 3] [4, 6] [8, 9]

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:

- Check if any two intervals overlap among a given set of intervals
- Find Non-overlapping intervals among a given set of intervals
- Find the point where maximum intervals overlap
- Count points covered by given intervals
- Find Intersection of all Intervals
- Maximum number of overlapping Intervals
- Count of available non-overlapping intervals to be inserted to make interval [0, R]
- Make the intervals non-overlapping by assigning them to two different processors
- Kth smallest element from an array of intervals
- Find least non-overlapping number from a given set of intervals
- How to check if two given sets are disjoint?
- Job Sequencing Problem | Set 2 (Using Disjoint Set)
- Count all disjoint pairs having absolute difference at least K from a given array
- Farthest index that can be reached from the Kth index of given array by given operations

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.