How many event pairs overlap
A room-booking audit counts every pair of events that clash so the worst offenders can be resolved.
- Every event has a half-open interval [start, end).
- A pair overlaps when the two intervals share at least one point.
- Count each unordered pair once.
ConflictPairs(events: list<Event>) → int
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public int ConflictPairs(List<Event> events) {
}
Worked examples
| Call | Result |
|---|---|
ConflictPairs(new List<Event> { new Event(1, 5), new Event(3, 8), new Event(10, 15) }) | 1 |
ConflictPairs(new List<Event> { new Event(1, 5), new Event(6, 10) }) | 0 |
ConflictPairs(new List<Event> { new Event(1, 5), new Event(2, 4), new Event(3, 8) }) | 3 |
ConflictPairs(new List<Event> { new Event(1, 10), new Event(2, 3), new Event(4, 5), new Event(6, 7) }) | 3 |
Hint
A brute-force double loop over all i < j pairs and the overlap test from bookings-overlap.
Reference solution in C#
public int ConflictPairs(List<Event> events) {
int count = 0;
for (int i = 0; i < events.Count; i++) {
for (int j = i + 1; j < events.Count; j++) {
if (events[i].Start < events[j].End && events[j].Start < events[i].End) count++;
}
}
return count;
}