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:
- Sort the given set of intervals according to starting time.
- Traverse all the set of intervals and check whether the consecutive intervals overlaps or not.
- 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.
- If the intervals overlaps, then check for next consecutive intervals.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
struct interval {
int start, end;
};
bool compareinterval(interval i1, interval i2)
{
return (i1.start < i2.start);
}
void findFreeinterval(interval arr[], int N)
{
if (N <= 0) {
return ;
}
vector<pair< int , int > > P;
sort(arr, arr + N, compareinterval);
for ( int i = 1; i < N; i++) {
int prevEnd = arr[i - 1].end;
int currStart = arr[i].start;
if (prevEnd < currStart) {
P.push_back({ prevEnd,
currStart });
}
}
for ( auto & it : P) {
cout << "[" << it.first << ", "
<< it.second << "]" << endl;
}
}
int main()
{
interval arr[] = { { 1, 3 },
{ 2, 4 },
{ 3, 5 },
{ 7, 9 } };
int N = sizeof (arr) / sizeof (arr[0]);
findFreeinterval(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static class Interval
{
int start, end;
Interval( int start, int end)
{
this .start = start;
this .end = end;
}
}
static void findFreeinterval( int [][] arr, int N)
{
if (N <= 0 )
{
return ;
}
ArrayList<Interval> p = new ArrayList<>();
Arrays.sort(arr, new Comparator< int []>()
{
public int compare( int [] a, int [] b)
{
return a[ 0 ] - b[ 0 ];
}
});
for ( int i = 1 ; i < N; i++)
{
int prevEnd = arr[i - 1 ][ 1 ];
int currStart = arr[i][ 0 ];
if (prevEnd < currStart)
{
Interval interval = new Interval(prevEnd,
currStart);
p.add(interval);
}
}
for ( int i = 0 ; i < p.size(); i++)
{
System.out.println( "[" + p.get(i).start +
", " + p.get(i).end + "]" );
}
}
public static void main(String[] args)
{
int [][] arr = { { 1 , 3 },
{ 2 , 4 },
{ 3 , 5 },
{ 7 , 9 } };
int N = arr.length;
findFreeinterval(arr, N);
}
}
|
Python3
def findFreeinterval(arr, N):
if N < 1 :
return
P = []
arr.sort(key = lambda a:a[ 0 ])
for i in range ( 1 , N):
prevEnd = arr[i - 1 ][ 1 ]
currStart = arr[i][ 0 ]
if prevEnd < currStart:
P.append([prevEnd, currStart])
for i in P:
print (i)
if __name__ = = "__main__" :
arr = [ [ 1 , 3 ], [ 2 , 4 ],
[ 3 , 5 ], [ 7 , 9 ] ]
N = len (arr)
findFreeinterval(arr, N)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
class Interval {
public int start, end;
public Interval( int start, int end)
{
this .start = start;
this .end = end;
}
}
static void FindFreeInterval( int [][] arr, int N)
{
if (N <= 0) {
return ;
}
List<Interval> p = new List<Interval>();
Array.Sort(arr, new Comparison< int []>(
(a, b) => a[0] - b[0]));
for ( int i = 1; i < N; i++) {
int prevEnd = arr[i - 1][1];
int currStart = arr[i][0];
if (prevEnd < currStart) {
Interval interval
= new Interval(prevEnd, currStart);
p.Add(interval);
}
}
for ( int i = 0; i < p.Count; i++) {
Console.WriteLine( "[" + p[i].start + ", "
+ p[i].end + "]" );
}
}
static void Main( string [] args)
{
int [][] arr
= { new int [] { 1, 3 }, new int [] { 2, 4 },
new int [] { 3, 5 }, new int [] { 7, 9 } };
int N = arr.Length;
FindFreeInterval(arr, N);
}
}
|
Javascript
<script>
function findFreeinterval(arr, N)
{
if (N <= 0) {
return ;
}
var P = [];
arr.sort((a,b) => a[0]-b[0])
for ( var i = 1; i < N; i++) {
var prevEnd = arr[i - 1][1];
var currStart = arr[i][0];
if (prevEnd < currStart) {
P.push([prevEnd,
currStart]);
}
}
P.forEach(it => {
document.write( "[" + it[0] + ", "
+ it[1] + "]" );
});
}
var arr = [ [ 1, 3 ],
[ 2, 4 ],
[ 3, 5 ],
[ 7, 9 ] ];
var N = arr.length;
findFreeinterval(arr, N);
</script>
|
Time Complexity: O(N*log N), where N is the number of set of intervals.
Auxiliary Space: O(N)
Last Updated :
03 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...