
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to count number of intervals which are intersecting at given point in Python
Suppose we have a list of intervals and a value called point. Each interval interval[i] contains [si, ei] represents start time and end time of interval i (both inclusive). We have to find the number of intervals that are intersecting at given point.
So, if the input is like intervals = [[2, 6],[4, 10],[5, 9],[11, 14]] point = 5, then the output will be 3, because at time 5, there are 3 intervals those are [3, 6], [4, 10], [5, 9]
To solve this, we will follow these steps −
count := 0
for each start time i and end time j in intervals, do
if point >= i and point <= j, then
count := count + 1
return count
Example
Let us see the following implementation to get better understanding
def solve(intervals, point): count = 0 for i, j in intervals: if point >= i and point <= j: count += 1 return count intervals = [[2, 6],[4, 10],[5, 9],[11, 14]] point = 5 print(solve(intervals, point))
Input
[[2, 6],[4, 10],[5, 9],[11, 14]], 5
Output
3
- Related Articles
- Program to count number of intervals that is totally contained inside other intervals in Python
- Find Intersecting Intervals in Python
- Count the number of intervals in which a given value lies in C++
- Program to count number of elements are placed at correct position in Python
- Program to count number of islands in a given matrix in Python
- Program to count number of square submatrices in given binary matrix in Python
- Python program to count number of vowels using set in a given string
- Program to count number of unique paths that includes given edges in Python
- Write a program in Python to count the number of digits in a given number N
- Program to count number of paths with cost k from start to end point in Python
- Python Program to Count Number of Non Leaf Nodes of a given Tree
- Program to count minimum number of animals which have no predator in Python
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- Program to find number of elements in all permutation which are following given conditions in Python

Advertisements