Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed file name #1509

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions Sorting Algorithms/Merge Overlapping Intervals/Program.c

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
##include <stdio.h>
#include <stdlib.h>

// Structure to represent an interval
typedef struct {
int start;
int end;
} Interval;

// Comparison function to sort intervals based on their start time
int compare(const void *a, const void *b) {
Interval *intervalA = (Interval *)a;
Interval *intervalB = (Interval *)b;
return intervalA->start - intervalB->start;
}

// Function to merge overlapping intervals
Interval* mergeIntervals(Interval* intervals, int intervalsSize, int* returnSize) {
if (intervalsSize == 0) {
*returnSize = 0;
return NULL;
}

// Sort the intervals based on the start time
qsort(intervals, intervalsSize, sizeof(Interval), compare);

// Allocate memory for the merged intervals
Interval* merged = (Interval*)malloc(intervalsSize * sizeof(Interval));
int mergedIndex = 0;

// Start with the first interval
merged[mergedIndex++] = intervals[0];

for (int i = 1; i < intervalsSize; i++) {
// If the current interval overlaps with the last merged interval
if (intervals[i].start <= merged[mergedIndex - 1].end) {
// Merge the intervals by updating the end time
if (intervals[i].end > merged[mergedIndex - 1].end) {
merged[mergedIndex - 1].end = intervals[i].end;
}
} else {
// No overlap, add the current interval to merged
merged[mergedIndex++] = intervals[i];
}
}

*returnSize = mergedIndex;
return merged;
}

// Helper function to print intervals
void printIntervals(Interval* intervals, int size) {
for (int i = 0; i < size; i++) {
printf("[%d, %d] ", intervals[i].start, intervals[i].end);
}
printf("\n");
}

// Main function to test the mergeIntervals function
int main() {
Interval intervals[] = {{1, 3}, {2, 6}, {8, 10}, {15, 18}};
int intervalsSize = sizeof(intervals) / sizeof(intervals[0]);
int returnSize;

Interval* mergedIntervals = mergeIntervals(intervals, intervalsSize, &returnSize);
printf("Merged Intervals: ");
printIntervals(mergedIntervals, returnSize);

// Free allocated memory
free(mergedIntervals);
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Approach
The steps are as follows:

First, we will group the closer intervals by sorting the given array of intervals(if it is not already sorted).

After that, we will select one interval at a time using a loop(say i) and insert it into our answer list(if the answer list is empty or the current interval cannot be merged with the last interval of the answer list). While traversing and inserting we will skip the intervals that lie in the last inserted interval of our answer list.
Now, for each interval arr[i], using another loop (say j) we are going to check the rest of the intervals(i.e. From index i+1 to n-1) if they can be merged with the selected interval.
Inside loop j, we will continue to merge all the intervals that lie in the selected interval.
Expand Down
Loading