Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if any interval completely overlaps the other in Python
In many programming problems, intervals are used to represent ranges such as time periods, numeric spans, etc. An interval is typically represented as a pair of numbers (start, end) where start ? end.
What is Complete Interval Overlap?
Complete overlap occurs when one interval fully contains another. For intervals X = (a1, a2) and Y = (b1, b2), X completely overlaps Y when a1 ? b1 and a2 ? b2. This means the entire range of Y lies within X.
Using Nested Loops
The most straightforward approach is to compare every pair of intervals to check if one completely contains another ?
def check_complete_overlap(intervals):
n = len(intervals)
for i in range(n):
for j in range(n):
if i != j:
a_start, a_end = intervals[i]
b_start, b_end = intervals[j]
if a_start <= b_start and a_end >= b_end:
return True
return False
# Example 1: Complete overlap exists
intervals1 = [(1, 5), (2, 4), (6, 7)]
print(f"Input: {intervals1}")
print(f"Output: {check_complete_overlap(intervals1)}")
Input: [(1, 5), (2, 4), (6, 7)] Output: True
Testing Non-overlapping Intervals
Let's test with intervals that don't have complete overlap ?
def check_complete_overlap(intervals):
n = len(intervals)
for i in range(n):
for j in range(n):
if i != j:
a_start, a_end = intervals[i]
b_start, b_end = intervals[j]
if a_start <= b_start and a_end >= b_end:
return True
return False
# Example 2: No complete overlap
intervals2 = [(1, 3), (4, 6), (7, 9)]
print(f"Input: {intervals2}")
print(f"Output: {check_complete_overlap(intervals2)}")
Input: [(1, 3), (4, 6), (7, 9)] Output: False
Optimized Approach Using Sorting
For better performance with large datasets, we can sort intervals by start time and use early termination ?
def check_complete_overlap_optimized(intervals):
# Sort intervals by start time
sorted_intervals = sorted(intervals, key=lambda x: x[0])
for i in range(len(sorted_intervals)):
for j in range(i + 1, len(sorted_intervals)):
a_start, a_end = sorted_intervals[i]
b_start, b_end = sorted_intervals[j]
# If current interval's end is before next's start, no overlap possible
if a_end < b_start:
break
# Check if i completely contains j
if a_start <= b_start and a_end >= b_end:
return True
# Check if j completely contains i
if b_start <= a_start and b_end >= a_end:
return True
return False
# Test with overlapping intervals
intervals3 = [(3, 8), (1, 5), (2, 4)]
print(f"Input: {intervals3}")
print(f"Output: {check_complete_overlap_optimized(intervals3)}")
Input: [(3, 8), (1, 5), (2, 4)] Output: True
Edge Cases
Let's test some edge cases including identical intervals ?
def test_edge_cases():
test_cases = [
([(1, 1), (1, 1)], "Identical single-point intervals"),
([(1, 5)], "Single interval"),
([(1, 3), (1, 3)], "Identical intervals"),
([(1, 5), (1, 4)], "Same start, different end"),
([(2, 5), (1, 5)], "Same end, different start")
]
for intervals, description in test_cases:
result = check_complete_overlap(intervals)
print(f"{description}: {intervals} ? {result}")
test_edge_cases()
Identical single-point intervals: [(1, 1), (1, 1)] ? True Single interval: [(1, 5)] ? False Identical intervals: [(1, 3), (1, 3)] ? True Same start, different end: [(1, 5), (1, 4)] ? True Same end, different start: [(2, 5), (1, 5)] ? True
Conclusion
Complete interval overlap checking is essential for scheduling and resource allocation problems. The nested loop approach works well for small datasets, while sorting-based optimization improves performance for larger inputs.
