Syntax error Program to count number of intervals which are intersecting at given point in Python

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
Updated on: 2021-10-11T07:37:17+05:30

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements