📖 Scenario: You are working on a calendar app that needs to combine overlapping meeting times into one continuous time slot. This helps users see their free time clearly.
🎯 Goal: Build a program that merges overlapping intervals from a list of meeting times and prints the merged intervals.
📋 What You'll Learn
Create a vector of pairs called intervals with given intervals
Create a variable merged to store merged intervals
Sort the intervals vector by start time
Use a for loop to merge overlapping intervals
Print the merged intervals in the format [start, end]
💡 Why This Matters
🌍 Real World
Merging intervals is useful in calendar apps, booking systems, and resource scheduling to avoid conflicts and show free time.
💼 Career
Understanding interval merging is important for software engineers working on scheduling, data processing, and optimization problems.
Progress0 / 4 steps
1
Create the initial intervals vector
Create a std::vector> called intervals with these exact intervals: {1, 3}, {2, 6}, {8, 10}, {15, 18}.
DSA C++
Hint
Use std::vector with std::pair<int, int> and initialize with the given intervals.
2
Add a vector to store merged intervals
Add a std::vector> called merged to store the merged intervals.
DSA C++
Hint
Declare an empty vector called merged to hold the merged intervals.
3
Sort and merge the intervals
Sort the intervals vector by start time using std::sort. Then use a for loop with variable interval to iterate over intervals. Inside the loop, merge overlapping intervals by comparing with the last interval in merged and update or add intervals accordingly.
DSA C++
Hint
Sort intervals first. Then for each interval, if merged is empty or no overlap, add it. Otherwise, update the end of last merged interval.
4
Print the merged intervals
Use a for loop with variable interval to iterate over merged and print each interval in the format [start, end] separated by spaces.
DSA C++
Hint
Loop over merged and print each interval with brackets and comma, separated by spaces.
Practice
(1/5)
1. What is the main purpose of the Merge Overlapping Intervals algorithm?
easy
A. To find the maximum interval length
B. To combine all intervals that overlap into a single continuous interval
C. To sort intervals in descending order
D. To remove all intervals that overlap
Solution
Step 1: Understand the problem goal
The algorithm aims to merge intervals that share common parts so they become one continuous interval.
Step 2: Clarify what merging means
Merging means combining overlapping intervals into one that covers the entire range of those intervals.
Final Answer:
To combine all intervals that overlap into a single continuous interval -> Option B
Hint: Think of joining overlapping time slots into one [OK]
Common Mistakes:
Confusing merging with sorting
Thinking it removes intervals instead of combining
Assuming it finds max length only
2. Which of the following C++ code snippets correctly sorts a vector of intervals by their start time before merging?
easy
A. std::sort(intervals.begin(), intervals.end(), [](auto &a, auto &b) { return a[0] < b[0]; });
B. std::sort(intervals.begin(), intervals.end(), [](auto &a, auto &b) { return a[1] > b[1]; });
C. std::sort(intervals.begin(), intervals.end(), [](auto &a, auto &b) { return a[1] < b[1]; });
D. std::sort(intervals.begin(), intervals.end(), [](auto &a, auto &b) { return a[0] > b[0]; });
Solution
Step 1: Identify sorting by start time
We need to sort intervals by their first element (start time) in ascending order.
Step 2: Check lambda comparator correctness
std::sort(intervals.begin(), intervals.end(), [](auto &a, auto &b) { return a[0] < b[0]; }); uses a lambda that compares a[0] < b[0], which sorts ascending by start time correctly.
Final Answer:
std::sort(intervals.begin(), intervals.end(), [](auto &a, auto &b) { return a[0] < b[0]; }); -> Option A
Quick Check:
Sort by start ascending = a[0] < b[0] [OK]
Hint: Sort intervals by start ascending using a lambda [OK]
Common Mistakes:
Sorting by end time instead of start time
Sorting in descending order
Using default sort without comparator on vector of vectors
3. What is the output of the following C++ code that merges overlapping intervals?