# Find the interval which contains maximum number of concurrent meetings

• Difficulty Level : Medium
• Last Updated : 08 Apr, 2022

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][0] 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][1] 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][0] 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[0] != b[0])``        ``return` `a[0] < b[0];``        ` `    ``return` `a[1] - b[1];``}` `// 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[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``(``auto` `k : meetings)``    ``{``        ` `        ``// Pop all meetings that end``        ``// before current meeting``        ``while` `(pq.size() > 0 &&``                    ``k[0] >= pq.top())``            ``pq.pop();` `        ``// Push current meeting end time``        ``pq.push(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.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);``    ``}``}`

## Python3

 `# Python implementation of the``# above approach` `# Function to find time slot of``    ``# maximum concurrent meeting``from` `functools ``import` `cmp_to_key` `def` `mycmp(a, b):``    ` `    ``return` `a[``0``] ``-` `b[``0``] ``if` `(a[``0``] !``=` `b[``0``]) ``else` `a[``1``] ``-` `b[``1``]` `def` `maxConcurrentMeetingSlot(meetings):` `    ``# Sort array by``    ``# start time of meeting``    ``meetings.sort(key ``=` `cmp_to_key(mycmp))``                        `  `    ``# Declare Minheap``    ``pq ``=` `[]` `    ``# Insert first meeting end time``    ``pq.append(meetings[``0``][``1``])``    ``pq.sort()``    ` `    ``# Initialize max_len,``    ``# max_start and max_end``    ``max_len,max_start,max_end ``=` `0``,``0``,``0` `    ``# Traverse over sorted array``    ``# to find required slot``    ``for` `k ``in` `range``(``len``(meetings)):` `        ``# Pop all meetings that end``        ``# before current meeting``        ``while` `(``len``(pq)!``=``0` `and` `meetings[k][``0``] >``=` `pq[``0``]):``            ``pq.pop(``0``)` `        ``# Push current meeting end time``        ``pq.append(meetings[k][``1``])``        ``pq.sort()``        ``# Update max_len, max_start``        ``# and max_end if size of``        ``# queue is greater than max_len``        ``if` `(``len``(pq) > max_len):``            ``max_len ``=` `len``(pq)``            ``max_start ``=` `meetings[k][``0``]``            ``max_end ``=` `pq[``0``]` `    ``# Print slot of maximum``    ``# concurrent meeting``    ``print``(f``"{max_start} {max_end}"``)` `# Driver Code``meetings ``=` `[[ ``100``, ``200` `],[ ``50``, ``300` `],[ ``300``, ``400` `]]` `# Function Call``maxConcurrentMeetingSlot(meetings)` `# This code is contributed by shinjanpatra`

## Javascript

 ``

Output:

`100 200`

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

My Personal Notes arrow_drop_up