Number of Intervals in which given value lies

Objective: Given a list of intervals with start and end for each interval. You have given a value V, write an algorithm to find the number of intervals in which the value V lies. 

Example:

Given Interval: [[1,7], [3,10], [12,15]]
Value : 6 lies in Intervals: 2

Given Interval: [[1,7], [3,10], [12,15]]
Value : 11 lies in Intervals: 0

Approach:

  1. Input: list of intervals and value V.
  2. Initialize count = 0.
  3. Iterate through the intervals, for each current interval
    1. Check if V lies in the current intervals mean start_of_current<=V and end_of_current>=V, increment the count.
  4. Return the count.

Complete Code:

Output:

Given Interval: [[1,7], [3,10], [12,15]]
Value : 6 lies in Intervals: 2

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

%d bloggers like this: