Bird
0
0
DSA Cprogramming~30 mins

Merge Overlapping Intervals in DSA C - Build from Scratch

Choose your learning style9 modes available
Merge Overlapping Intervals
📖 Scenario: You are working on a calendar app that manages meeting times. Sometimes meetings overlap, and you want to combine those overlapping meetings into one longer meeting to avoid confusion.
🎯 Goal: Build a program that merges overlapping intervals from a list of meeting times and prints the merged intervals.
📋 What You'll Learn
Create an array of intervals with exact values
Add a variable to store the number of intervals
Implement the logic to merge overlapping intervals
Print the merged intervals in the format: [start, end]
💡 Why This Matters
🌍 Real World
Merging overlapping intervals is useful in calendar apps, booking systems, and timeline visualizations to avoid conflicts and show clear schedules.
💼 Career
Understanding interval merging helps in software development roles involving scheduling, data processing, and algorithm design.
Progress0 / 4 steps
1
Create the initial intervals array
Create an array called intervals of type int[][2] with these exact intervals: {1, 3}, {2, 6}, {8, 10}, {15, 18}.
DSA C
Hint

Use curly braces to define each interval inside the array.

2
Add the number of intervals variable
Create an int variable called n and set it to 4, the number of intervals in intervals.
DSA C
Hint

Count how many intervals are in the array and assign that number to n.

3
Implement merging logic
Write code to merge overlapping intervals from intervals using n. Store merged intervals in merged array and keep count in mergedCount. Use a simple sorting and merging approach.
DSA C
Hint

Sort intervals by start time, then merge by comparing current interval start with last merged interval end.

4
Print merged intervals
Print each merged interval from merged array using mergedCount. Format each interval as [start, end] on its own line.
DSA C
Hint

Use a for loop to print each merged interval with printf.