- Related Questions & Answers
- How to check if a given directory contains any other directory in Python?
- Program to check two rectangular overlaps or not in Python
- Check if tuple has any None value in Python
- Check if one list is subset of other in Python
- Check if one tuple is subset of other in Python
- Check if any permutation of N equals any power of K in Python
- Check if any alert exists using selenium with python.
- Python - Check if two lists have any element in common
- Python Check if suffix matches with any string in given list?
- Check if strings are rotations of each other or not in Python
- Check if one of the numbers is one’s complement of the other in Python
- Python program to check if a string contains any unique character
- Check if any anagram of a string is palindrome or not in Python
- Check if any large number is divisible by 17 or not in Python
- Check if any large number is divisible by 19 or not in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose, we are given a set of intervals that consists of values (a,b) where a represents the starting time and b represents the ending time of an event. Our task is to check whether any of these intervals completely overlap any other interval in this set. If any of the intervals overlap, we return the result as True, otherwise we return False.

So, if the input is like [(4,6), (10,12), (7,9), (13,16)], then the output will be False. If the input is like [(4,6), (4,9), (7,11), (5,8)], then the output will be True.

To solve this, we will follow these steps −

- sort the list intervals
- for i in range 1 to size of intervals, do
- if intervals[i, 1] <= intervals[i- 1, 1], then
- return True

- return False

- if intervals[i, 1] <= intervals[i- 1, 1], then

Let us see the following implementation to get better understanding −

def solve(intervals): intervals.sort() for i in range(1, len(intervals)): if intervals[i][1] <= intervals[i- 1][1]: return True return False intervals = [(4,6),(10,12),(7,9),(13,16)] intervals2 = [(4,6), (4,9), (7,11), (5,8)] print(solve(intervals)) print(solve(intervals2))

[(4,6),(10,12),(7,9),(13,16)] [(4,6), (4,9), (7,11), (5,8)]

False True

Advertisements