-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntervals_Intersection.cpp
More file actions
67 lines (53 loc) · 1.63 KB
/
Intervals_Intersection.cpp
File metadata and controls
67 lines (53 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Problem Statement: Given two lists of intervals, find the intersection of these two lists. Each list consists of disjoint intervals sorted on their start time.
Example:
Input: arr1=[[1, 3], [5, 6], [7, 9]], arr2=[[2, 3], [5, 7]]
Output: [2, 3], [5, 6], [7, 7]
Explanation: The output list contains the common intervals between the two lists.
*/
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
struct Interval
{
int s,e;
};
vector<Interval> Make_Intersection(Interval arr1[], Interval arr2[],int n1, int n2 )
{
vector<Interval> Intersection;
int i=0,j=0;
while(i<n1 && j<n2)
{
if((arr1[i].s>=arr2[j].s && arr1[i].s<=arr2[j].e )||
(arr2[j].s>=arr1[i].s && arr2[j].s<=arr1[i].e))
{
Intersection.push_back({max(arr1[i].s,arr2[j].s),min(arr1[i].e,arr2[j].e)});
}
if(arr1[i].e<arr2[j].e)
{
i++;
}
else
{
j++;
}
}
return Intersection;
}
int main()
{
Interval arr1[]={{1,3},{5,7},{9,12}};
Interval arr2[]={{5,10}};
int n1=sizeof(arr1)/sizeof(arr1[0]);
int n2=sizeof(arr2)/sizeof(arr2[0]);
vector<Interval> result;
result=Make_Intersection(arr1,arr2,n1,n2);
for(auto interval: result )
{
cout<<"["<<interval.s<<","<<interval.e<<"]";
}
}
/*
Time Complexity Taken by this algorithm is O(N+M) where and N & M are total number of intervals in the input arrays respectively
Space Complexity : If space needed for the result list is ignored , the algorithm runs in O(1) , constant space.
*/