Maximal Disjoint Intervals
Last Updated :
20 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
bool sortbysec( const pair< int , int >& a,
const pair< int , int >& b)
{
return (a.second < b.second);
}
void maxDisjointIntervals(vector<pair< int , int > > list)
{
sort(list.begin(), list.end(), sortbysec);
cout << "[" << list[0].first << ", "
<< list[0].second << "]" << endl;
int r1 = list[0].second;
for ( int i = 1; i < list.size(); i++) {
int l1 = list[i].first;
int r2 = list[i].second;
if (l1 > r1) {
cout << "[" << l1 << ", "
<< r2 << "]" << endl;
r1 = r2;
}
}
}
int main()
{
int N = 4;
vector<pair< int , int > > intervals = { { 1, 4 },
{ 2, 3 },
{ 4, 6 },
{ 8, 9 } };
maxDisjointIntervals(intervals);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG
{
static class Pair implements Comparable<Pair>
{
int first, second;
Pair( int f, int s)
{
first = f;
second = s;
}
@Override
public int compareTo(Pair o)
{
if ( this .second > o.second)
return 1 ;
else if ( this .second == o.second)
return 0 ;
return - 1 ;
}
}
static void maxDisjointIntervals(Pair []list)
{
Collections.sort(Arrays.asList(list));
System.out.print( "[" + list[ 0 ].first+ ", "
+ list[ 0 ].second+ "]" + "\n" );
int r1 = list[ 0 ].second;
for ( int i = 1 ; i < list.length; i++) {
int l1 = list[i].first;
int r2 = list[i].second;
if (l1 > r1) {
System.out.print( "[" + l1+ ", "
+ r2+ "]" + "\n" );
r1 = r2;
}
}
}
public static void main(String[] args)
{
int N = 4 ;
Pair []intervals = { new Pair( 1 , 4 ),
new Pair( 2 , 3 ),
new Pair( 4 , 6 ),
new Pair( 8 , 9 ) };
maxDisjointIntervals(intervals);
}
}
|
Python3
def maxDisjointIntervals(list_):
list_.sort(key = lambda x: x[ 1 ])
print ( "[" , list_[ 0 ][ 0 ], ", " , list_[ 0 ][ 1 ], "]" )
r1 = list_[ 0 ][ 1 ]
for i in range ( 1 , len (list_)):
l1 = list_[i][ 0 ]
r2 = list_[i][ 1 ]
if l1 > r1:
print ( "[" , l1, ", " , r2, "]" )
r1 = r2
if __name__ = = "__main__" :
N = 4
intervals = [ [ 1 , 4 ], [ 2 , 3 ],
[ 4 , 6 ], [ 8 , 9 ] ]
maxDisjointIntervals(intervals)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void Main( string [] args)
{
int N = 4;
List<Tuple< int , int >> intervals = new List<Tuple< int , int >>()
{
new Tuple< int , int >(1, 4),
new Tuple< int , int >(2, 3),
new Tuple< int , int >(4, 6),
new Tuple< int , int >(8, 9)
};
MaxDisjointIntervals(intervals);
}
private static void MaxDisjointIntervals(List<Tuple< int , int >> list)
{
list.Sort((a, b) => a.Item2.CompareTo(b.Item2));
Console.WriteLine( "[" + list[0].Item1 + ", " + list[0].Item2 + "]" );
int r1 = list[0].Item2;
for ( int i = 1; i < list.Count; i++)
{
int l1 = list[i].Item1;
int r2 = list[i].Item2;
if (l1 > r1)
{
Console.WriteLine( "[" + l1 + ", " + r2 + "]" );
r1 = r2;
}
}
}
}
|
Javascript
<script>
function maxDisjointIntervals(list)
{
list.sort((a,b)=> a[1]-b[1]);
document.write( "[" + list[0][0] + ", "
+ list[0][1] + "]" + "<br>" );
var r1 = list[0][1];
for ( var i = 1; i < list.length; i++) {
var l1 = list[i][0];
var r2 = list[i][1];
if (l1 > r1) {
document.write( "[" + l1 + ", "
+ r2 + "]" + "<br>" );
r1 = r2;
}
}
}
var N = 4;
var intervals = [ [ 1, 4 ],
[ 2, 3 ],
[ 4, 6 ],
[ 8, 9 ] ];
maxDisjointIntervals(intervals);
</script>
|
Output:
[2, 3]
[4, 6]
[8, 9]
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...