From 8932d696dd954ab8e01163a690aa8ca6f2d06be3 Mon Sep 17 00:00:00 2001 From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:24:33 +0530 Subject: [PATCH 1/2] Made the modification #677 --- .../Merge Overlapping Intervals/Program.c | 42 ----------- .../Program_merge.c | 72 +++++++++++++++++++ .../Merge Overlapping Intervals/Readme.md | 1 + 3 files changed, 73 insertions(+), 42 deletions(-) delete mode 100644 Sorting Algorithms/Merge Overlapping Intervals/Program.c create mode 100644 Sorting Algorithms/Merge Overlapping Intervals/Program_merge.c diff --git a/Sorting Algorithms/Merge Overlapping Intervals/Program.c b/Sorting Algorithms/Merge Overlapping Intervals/Program.c deleted file mode 100644 index 095b1f3c..00000000 --- a/Sorting Algorithms/Merge Overlapping Intervals/Program.c +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include -using namespace std; - -vector> mergeOverlapInter(vector>& arr) { - - // Sort intervals based on start values - sort(arr.begin(), arr.end()); - - vector> out; - out.push_back(arr[0]); - - for (int i = 1; i < arr.size(); i++) { - vector& last = out.back(); - vector& curr = arr[i]; - - // If current overlaps with the last - // merged, merge them - if (curr[0] <= last[1]) - last[1] = max(last[1], curr[1]); - - else // Add current to the result - out.push_back(curr); - } - - return out; -} - -// Driver Code -int main() { - vector> arr = {{6, 12}, {1, 9}, {2, 4}, {4, 8}}; - vector> out = mergeOverlapInter(arr); - - cout << "The Merged Overlapped Intervals are: "; - for (const vector& interval : out) { - cout << "[" << interval[0] << ", " << interval[1] << "] "; - } - cout << endl; - - return 0; -} \ No newline at end of file diff --git a/Sorting Algorithms/Merge Overlapping Intervals/Program_merge.c b/Sorting Algorithms/Merge Overlapping Intervals/Program_merge.c new file mode 100644 index 00000000..1ea3601b --- /dev/null +++ b/Sorting Algorithms/Merge Overlapping Intervals/Program_merge.c @@ -0,0 +1,72 @@ +##include +#include + +// 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; +} \ No newline at end of file diff --git a/Sorting Algorithms/Merge Overlapping Intervals/Readme.md b/Sorting Algorithms/Merge Overlapping Intervals/Readme.md index e9121987..e267743f 100644 --- a/Sorting Algorithms/Merge Overlapping Intervals/Readme.md +++ b/Sorting Algorithms/Merge Overlapping Intervals/Readme.md @@ -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. From e7e6020a6d589b441f0fa468eaf23abf49b54133 Mon Sep 17 00:00:00 2001 From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com> Date: Wed, 30 Oct 2024 20:08:05 +0530 Subject: [PATCH 2/2] Changed file name --- .../Overlapping Intervales.c} | 0 .../Readme.md | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Sorting Algorithms/{Merge Overlapping Intervals/Program_merge.c => Merge_Overlapping_Intervals/Overlapping Intervales.c} (100%) rename Sorting Algorithms/{Merge Overlapping Intervals => Merge_Overlapping_Intervals}/Readme.md (100%) diff --git a/Sorting Algorithms/Merge Overlapping Intervals/Program_merge.c b/Sorting Algorithms/Merge_Overlapping_Intervals/Overlapping Intervales.c similarity index 100% rename from Sorting Algorithms/Merge Overlapping Intervals/Program_merge.c rename to Sorting Algorithms/Merge_Overlapping_Intervals/Overlapping Intervales.c diff --git a/Sorting Algorithms/Merge Overlapping Intervals/Readme.md b/Sorting Algorithms/Merge_Overlapping_Intervals/Readme.md similarity index 100% rename from Sorting Algorithms/Merge Overlapping Intervals/Readme.md rename to Sorting Algorithms/Merge_Overlapping_Intervals/Readme.md