-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlap.c
49 lines (45 loc) · 800 Bytes
/
overlap.c
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
#include <stdio.h>
struct node{
int s, e;
};
void sort_arr(struct node arr[], int n){
int i, j;
struct node temp;
for(i = 0; i < n - 1; i++){
for(j = i + 1; j < n; j++){
if(arr[i].s > arr[j].s){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
struct node arr[20];
void main(){
int n, i, j;
printf("Enter the number of intervals\n");
scanf("%d", &n);
struct node temp;
printf("Enter the intervals\n");
for(i = 0; i < n; i++){
scanf("%d %d", &arr[i].s, &arr[i].e);
}
sort_arr(arr, n);
i = 0;
j = 0;
printf("\nMerged Intervals are :\n");
for(i = 0; i < n; i++){
temp.s = arr[i].s;
for(j = i + 1; j < n; j++){
if(arr[i].e > arr[j].s){
i = j;
}
else{
break;
}
}
temp.e = arr[i].e;
printf("(%d, %d)\n", temp.s, temp.e);
}
}