Related Articles

# Find the interval which contains maximum number of concurrent meetings

• Difficulty Level : Medium
• Last Updated : 21 Jun, 2021

Given a two dimensional array arr[][] of dimensions N * 2 which contains the starting and ending time for N meetings on a given day. The task is to print a list of time slots during which most number of concurrent meetings can be held.

Examples:

Input: arr[][] = {{100, 300}, {145, 215}, {200, 230}, {215, 300}, {215, 400}, {500, 600}, {600, 700}}
Output: [215, 230]
Explanation:
The given 5 meetings overlap at {215, 230}.

Input: arr[][] = {{100, 200}, {50, 300}, {300, 400}}
Output: [100, 200]

Approach: The idea is to use a Min-Heap to solve this problem. Below are the steps:

1. Sort the array based on the start time of meetings.
2. Initialize a min-heap.
3. Initialize variables max_len, max_start and max_end to store maximum size of min heap, start time and end time of concurrent meetings respectively.
4. Iterate over the sorted array and keep popping from min_heap until arr[i] becomes smaller than the elements of the min_heap, i.e. pop all the meetings having ending time smaller than the starting time of current meeting, and push arr[i] in to min_heap.
5. If the size of min_heap exceeds max_len, then update max_len = size(min_heap), max_start = meetings[i] and max_end = min_heap_element.
6. Return the value of max_start and max_end at the end.

Below is the implementation of the above approach:

## C++14

 `// C++14 implementation of the` `// above approach` `#include ` `using` `namespace` `std;`   `bool` `cmp(vector<``int``> a,vector<``int``> b)` `{`   `    ``if``(a != b)` `        ``return` `a < b;` `        `  `    ``return` `a - b;` `}`   `// Function to find time slot of` `// maximum concurrent meeting` `void` `maxConcurrentMeetingSlot(` `    ``vector> meetings)` `{` `    `  `    ``// Sort array by` `    ``// start time of meeting` `    ``sort(meetings.begin(), meetings.end(), cmp);`   `    ``// Declare Minheap` `    ``priority_queue<``int``, vector<``int``>,` `                       ``greater<``int``>> pq;` `    `  `    ``// Insert first meeting end time` `    ``pq.push(meetings);`   `    ``// Initialize max_len,` `    ``// max_start and max_end` `    ``int` `max_len = 0, max_start = 0;` `    ``int` `max_end = 0;`   `    ``// Traverse over sorted array` `    ``// to find required slot` `    ``for``(``auto` `k : meetings)` `    ``{` `        `  `        ``// Pop all meetings that end` `        ``// before current meeting` `        ``while` `(pq.size() > 0 && ` `                    ``k >= pq.top())` `            ``pq.pop();`   `        ``// Push current meeting end time` `        ``pq.push(k);`   `        ``// Update max_len, max_start` `        ``// and max_end if size of` `        ``// queue is greater than max_len` `        ``if` `(pq.size() > max_len)` `        ``{` `            ``max_len = pq.size();` `            ``max_start = k;` `            ``max_end = pq.top();` `        ``}` `    ``}`   `    ``// Print slot of maximum` `    ``// concurrent meeting` `    ``cout << max_start << ``" "` `<< max_end;` `}`   `// Driver Code` `int` `main()` `{` `    `  `    ``// Given array of meetings` `    ``vector> meetings = { { 100, 200 },` `                                     ``{ 50, 300 },` `                                     ``{ 300, 400 } };` `                                     `  `    ``// Function call` `    ``maxConcurrentMeetingSlot(meetings);` `}`   `// This code is contributed by mohit kumar 29`

## Java

 `// Java implementation of the` `// above approach`   `import` `java.util.*;` `import` `java.lang.*;`   `class` `GFG {`   `    ``// Function to find time slot of` `    ``// maximum concurrent meeting` `    ``static` `void` `maxConcurrentMeetingSlot(` `        ``int``[][] meetings)` `    ``{`   `        ``// Sort array by` `        ``// start time of meeting` `        ``Arrays.sort(meetings,` `                    ``(a, b)` `                        ``-> (a[``0``] != b[``0``])` `                               ``? a[``0``] - b[``0``]` `                               ``: a[``1``] - b[``1``]);`   `        ``// Declare Minheap` `        ``PriorityQueue pq` `            ``= ``new` `PriorityQueue<>();`   `        ``// Insert first meeting end time` `        ``pq.add(meetings[``0``][``1``]);`   `        ``// Initialize max_len,` `        ``// max_start and max_end` `        ``int` `max_len = ``0``, max_start = ``0``;` `        ``int` `max_end = ``0``;`   `        ``// Traverse over sorted array` `        ``// to find required slot` `        ``for` `(``int``[] k : meetings) {`   `            ``// Pop all meetings that end` `            ``// before current meeting` `            ``while` `(!pq.isEmpty()` `                   ``&& k[``0``] >= pq.peek())` `                ``pq.poll();`   `            ``// Push current meeting end time` `            ``pq.add(k[``1``]);`   `            ``// Update max_len, max_start` `            ``// and max_end if size of` `            ``// queue is greater than max_len` `            ``if` `(pq.size() > max_len) {` `                ``max_len = pq.size();` `                ``max_start = k[``0``];` `                ``max_end = pq.peek();` `            ``}` `        ``}`   `        ``// Print slot of maximum` `        ``// concurrent meeting` `        ``System.out.println(` `            ``max_start + ``" "` `+ max_end);` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``// Given array of meetings` `        ``int` `meetings[][]` `            ``= { { ``100``, ``200` `}, ` `                ``{ ``50``, ``300` `}, ` `                ``{ ``300``, ``400` `} };`   `        ``// Function Call` `        ``maxConcurrentMeetingSlot(meetings);` `    ``}` `}`

## Javascript

 ``

Output:

`100 200`

Time Complexity: O(N * logN)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up
Recommended Articles
Page :