Consider an event where a log register is maintained containing the guest’s arrival and departure times. Given an array of arrival and departure times from entries in the log register, find the point when there were maximum guests present in the event.

Note that if an arrival and departure event coincides, the arrival time is preferred over the departure time.

 
For example,

Input:
 
arrival = { 1, 2, 4, 7, 8, 12 }
departure = { 2, 7, 8, 12, 10, 15 }
 
Output: Maximum number of guests is 3, present at time 7

Maximum Overlapping Interval

 
Related Posts:

Find minimum platforms needed to avoid delay in the train arrival

1. Using Brute-Force (Quadratic Time Solution)

The idea is to find time t when the last guest leaves the event and create a count array of size t+2. Then fill the count array with the guest’s count using the array index to store time, i.e., for an interval [x, y], the count array is filled in a way that all values between the indices x and y are incremented by 1.

After the count array is filled with each event timings, find the maximum element’s index in the count array. This index would be the time when there were maximum guests present in the event.

The time complexity of this approach is quadratic and requires extra space for the count array. Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

Event Time: 7
The maximum number of guests is 3

Java


Download  Run Code

Output:

Event Time: 7
The maximum number of guests is 3

Python


Download  Run Code

Output:

Event Time: 7
The maximum number of guests is 3

2. Using Sorting (Linearithmic Time Solution)

The idea is to sort the arrival and departure times of guests and use the merge routine of the merge sort algorithm to process them together as a single sorted array of events. We maintain a counter to store the count number of guests present at the event at any point. While processing all events (arrival & departure) in sorted order,

  • If the next event is arrival, increase the number of guests by one and update the maximum guest’s count found so far if the current guest’s count is more.
  • If the next event is a departure, decrease the guest’s count by 1.

By following this process, we can keep track of the total number of guests at any time (guests that have arrived but not left). The time complexity of this approach is O(n.log(n)) and doesn’t require any extra space, where n is the total number of guests. Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

Event Time: 7
The maximum number of guests is 3

Java


Download  Run Code

Output:

Event Time: 7
The maximum number of guests is 3

Python


Download  Run Code

Output:

Event Time: 7
The maximum number of guests is 3

3. Using Extra Space (Linear Time Solution)

We can improve solution #1 to run in linear time. The idea is to store only arrival and departure times in a count array instead of filling all values in an interval. This is done by increasing the value at the arrival time by one and decreasing the value after departure time by one.

After all guest logs are processed, perform a prefix sum computation to determine the exact guest count at each point, and get the index with maximum value. This index would be the time when there were maximum guests present in the event.

The time complexity of the above solution is O(n), but requires O(n) extra space. Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

Event Time: 7
The maximum number of guests is 3

Java


Download  Run Code

Output:

Event Time: 7
The maximum number of guests is 3

Python


Download  Run Code

Output:

Event Time: 7
The maximum number of guests is 3