Non Overlapping Intervals Minimum Removal in DSA C - Time & Space Complexity
We want to find how fast the algorithm runs when removing intervals to avoid overlaps.
How does the time needed grow as the number of intervals increases?
Analyze the time complexity of the following code snippet.
// Sort intervals by end time
qsort(intervals, n, sizeof(Interval), compareEnd);
int count = 0;
int end = intervals[0].end;
for (int i = 1; i < n; i++) {
if (intervals[i].start < end) {
count++;
} else {
end = intervals[i].end;
}
}
return count;
This code sorts intervals by their end time, then counts how many intervals must be removed to avoid overlaps.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting the intervals array.
- How many times: Sorting compares elements multiple times, then a single loop runs through all intervals once.
As the number of intervals grows, sorting takes more time, and the loop checks each interval once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 to 40 operations |
| 100 | About 700 to 800 operations |
| 1000 | About 10,000 to 12,000 operations |
Pattern observation: Operations grow faster than the input size but not as fast as its square; sorting dominates growth.
Time Complexity: O(n log n)
This means the time needed grows a bit faster than the number of intervals because sorting takes more work as input grows.
[X] Wrong: "The loop checking overlaps is the slowest part, so complexity is O(n)."
[OK] Correct: Sorting the intervals takes more time than the single loop, so it controls the overall speed.
Understanding this helps you explain how sorting affects performance and why greedy choices after sorting are efficient.
"What if the intervals were already sorted by start time? How would the time complexity change?"
Practice
Non Overlapping Intervals Minimum Removal problem?Solution
Step 1: Understand the problem goal
The problem asks to remove the fewest intervals so that no two intervals overlap.Step 2: Clarify what non-overlapping means
Non-overlapping intervals do not share any common points except possibly at the boundary.Final Answer:
To find the minimum number of intervals to remove so that the rest do not overlap -> Option AQuick Check:
Minimum removal = non-overlapping intervals [OK]
- Confusing removal with merging intervals
- Thinking the goal is to count overlaps
- Assuming intervals can be rearranged
Solution
Step 1: Identify sorting criteria for minimum removal
Sorting intervals by their end time helps greedily select intervals that finish earliest.Step 2: Match sorting function
The functioncompareEndsorts by end time, which is correct for this problem.Final Answer:
qsort(intervals, n, sizeof(Interval), compareEnd); -> Option BQuick Check:
Sort by end time = compareEnd [OK]
- Sorting by start time instead of end time
- Sorting by interval length which is irrelevant
- Sorting in descending order of start time
typedef struct { int start; int end; } Interval;
int minRemoval(Interval intervals[], int n) {
qsort(intervals, n, sizeof(Interval), compareEnd);
int count = 0, end = intervals[0].end;
for (int i = 1; i < n; i++) {
if (intervals[i].start < end) {
count++;
} else {
end = intervals[i].end;
}
}
return count;
}
// Intervals: {{1,3}, {2,4}, {3,5}}
int main() {
Interval intervals[] = {{1,3}, {2,4}, {3,5}};
int n = 3;
printf("%d", minRemoval(intervals, n));
return 0;
}Solution
Step 1: Sort intervals by end time
Sorted intervals by end: {1,3}, {2,4}, {3,5}Step 2: Count overlaps and removals
Start with end=3. Interval {2,4} starts at 2 < 3, so count=1. Interval {3,5} starts at 3 >= 3, update end=5.Final Answer:
1 -> Option DQuick Check:
One overlap removed = 1 [OK]
- Not sorting intervals before counting
- Counting intervals that do not overlap
- Updating end incorrectly
int minRemoval(Interval intervals[], int n) {
qsort(intervals, n, sizeof(Interval), compareStart);
int count = 0, end = intervals[0].end;
for (int i = 1; i < n; i++) {
if (intervals[i].start < end) {
count++;
} else {
end = intervals[i].end;
}
}
return count;
}Solution
Step 1: Check sorting criteria
The function sorts intervals by start time, but it should sort by end time for correct greedy selection.Step 2: Understand impact of wrong sorting
Sorting by start time can cause incorrect overlap counting and wrong removal count.Final Answer:
Using compareStart instead of compareEnd in qsort -> Option AQuick Check:
Sort by end time needed [OK]
- Sorting by start time
- Incorrect loop index
- Wrong update of end variable
{{1,2}, {2,3}, {3,4}, {1,3}}, what is the minimum number of intervals to remove to make the rest non-overlapping?Solution
Step 1: Sort intervals by end time
Sorted intervals: {1,2}, {2,3}, {1,3}, {3,4}Step 2: Select intervals greedily and count removals
Select {1,2} first (end=2). {2,3} starts at 2 >= 2, select it (end=3). {1,3} starts at 1 < 3, remove it (count=1). {3,4} starts at 3 >= 3, select it.Final Answer:
1 -> Option CQuick Check:
One removal needed to avoid overlap [OK]
- Removing too many intervals
- Not sorting intervals before processing
- Confusing overlapping intervals with touching intervals
Ready to prove it?
Pass the test and this pattern moves unknown
