-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_2.cpp
57 lines (52 loc) · 1.32 KB
/
code_2.cpp
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
//
// code_2.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
struct Interval {
int start, end;
};
bool compareInterval(Interval i1, Interval i2) {
return (i1.start < i2.start);
}
void mergeIntervals(vector<Interval>arr) {
int n = int(arr.size());
int index = 0;
for (int i = 0; i < n; i++) {
if (index != 0 && arr[index-1].start <= arr[i].end) {
while (index != 0 && arr[index-1].start <= arr[i].end) {
arr[index-1].end = max(arr[index-1].end, arr[i].end);
arr[index-1].start = min(arr[index-1].start, arr[i].start);
index--;
}
}
else {
arr[index] = arr[i];
}
index++;
}
cout << "\nMerged Intervals\t:\t";
for (int i = 0; i < index; i++) {
cout << "[" << arr[i].start << ", " << arr[i].end << "] ";
}
cout << endl;
return;
}
int main() {
int n;
cout << "\nEnter Number of Intervals\t:\t";
cin >> n;
vector<Interval> array(n);
cout << "\nEnter Range (L,R) Form\n";
for (int i = 0; i < n; i++ ) {
cin >> array[i].start;
cin >> array[i].end;
}
mergeIntervals(array);
return 0;
}