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
Java 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
int conflictPairs(List<Event> events) {
}
Worked examples
| Call | Result |
|---|---|
conflictPairs(Main.<Event>ls(new Event(1, 5), new Event(3, 8), new Event(10, 15))) | 1 |
conflictPairs(Main.<Event>ls(new Event(1, 5), new Event(6, 10))) | 0 |
conflictPairs(Main.<Event>ls(new Event(1, 5), new Event(2, 4), new Event(3, 8))) | 3 |
conflictPairs(Main.<Event>ls(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 Java
int conflictPairs(List<Event> events) {
int count = 0;
for (int i = 0; i < events.size(); i++) {
for (int j = i + 1; j < events.size(); j++) {
Event a = events.get(i);
Event b = events.get(j);
if (a.start < b.end && b.start < a.end) count++;
}
}
return count;
}