-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverlapping Intervals.cpp
More file actions
44 lines (39 loc) · 872 Bytes
/
Copy pathOverlapping Intervals.cpp
File metadata and controls
44 lines (39 loc) · 872 Bytes
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
#include<bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
void mergeInterval(int);
int main(){
int t, n;
cin >> t;
while(t--){
cin >> n;
mergeInterval(n);
cout << endl;
}
return 0;
}
void mergeInterval(int n){
pii arr[n];
for(int i = 0; i < n; i++){
int a, b;
cin >> a >> b;
arr[i] ={a, b};
}
sort(arr, arr + n);
int i = 0, j, max = arr[i].second;
for(j = 1; j < n; j++){
if(arr[j].first <= max){
if(max < arr[j].second)
max = arr[j].second;
continue;
}
else{
cout << arr[i].first << " " << max << " ";
i = j;
max = arr[i].second;
}
}
if(max < arr[j - 1].second)
max = arr[j - 1].second;
cout << arr[i].first << " " << max << " ";
}