-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathMerge_Interval.c
60 lines (60 loc) · 1.71 KB
/
Merge_Interval.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
50
51
52
53
54
55
56
57
58
59
60
/*Name : Atul Kumar
Github username : atul1510
Repositary name : Algorithms
*/
//Problem Statement : Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals,
//and return an array of the non-overlapping intervals that cover all the intervals in the input.
// C program to merge overlapping Intervals in
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// An Interval
typedef struct IntL {
int x, z;
} Interval;
// Function used in sort
int mp(const void* a, const void* b)
{
Interval* data_1 = (Interval*)a;
Interval* data_2 = (Interval*)b;
return (data_1->x - data_2->x);
}
// Find maximum between two numbers.
int max(int num1, int num2)
{
return (num1 > num2) ? num1 : num2;
}
void MI(Interval arr[], int n)
{
// Sort Intervals in increasing order of
// start time
qsort(arr, n, sizeof(Interval), mp);
int index = 0; // Stores index of last element
// in output array (modified arr[])
// Traverse all input Intervals
for (int i = 1; i < n; i++) {
// If this is not first Interval and overlaps
// with the previous one
if (arr[index].z >= arr[i].x) {
// Merge previous and current Intervals
arr[index].z = max(arr[index].z, arr[i].z);
}
else {
index++;
arr[index] = arr[i];
}
}
// Now arr[0..index-1] stores the merged Intervals
printf("\n The Merged Intervals are: ");
for (int i = 0; i <= index; i++)
printf("[%d, %d]", arr[i].x, arr[i].z);
}
// Driver program
int main()
{
Interval arr[]
= { { 1, 3 }, { 2, 6 }, { 8, 10 }, { 15, 18 } };
int n = sizeof(arr) / sizeof(arr[0]);
MI(arr, n);
return 0;
}