# Length of smallest meeting that can be attended

• Difficulty Level : Expert
• Last Updated : 22 Jun, 2021

Given a 2D array arr[][] of the form {start, end} representing the start and end time of N meetings, also given two arrays entrance[] and exist[] representing the opening and closing times of the meeting room respectively, the task is to find the minimum time for which a meeting can be attended. If it is not possible to attend any meeting, then print -1.

Examples:

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.

Input: arr[][] = {{15, 19}, {5, 10}, {7, 25}}, entrance[] = {4, 13, 25, 2}, exist[] = {10, 21}
Output: 6
Explanation:
Meeting 1: Enter at the time 13, attend the meeting in the interval (15, 19) and exit at the time 21. Therefore, total time spent in the meeting = 21 – 13 = 8.
Meeting 2: Enter at the time 4, attend the meeting in (5, 10) and exit at the time 10. Therefore, total time spent in the meeting = 10 – 4 = 6.
Meeting 3: Enter at the time 4, attend the meeting in the interval (7, 25). But after the time 25 there is no closing time. Therefore, total time spent is infinite.
Therefore, minimum units of time that can be spent to attend a meeting is 6.

Input: arr[][] = {{1, 2}}, entrance[] = {1, 2}, exist[] = {3, 4}
Output:

Naive Approach: The simplest approach to solve this problem is to traverse arr[][] and for each interval {starti, endi}, find the value which just smaller than or equal to arr[i][0] in the array entrance[]. Also, find the value which is just greater than or equal to arr[i][1] in the array exist[]. Finally, print the minimum time to attend exactly one meeting.

Time Complexity: O(N*max(P, M)), where M and P are the size of entrance[]and exist[] arrays.
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach the idea is to use Sorting Algorithm and Binary Search technique
Follow the steps below to solve the problem:

1. Sort the arrays entrance[] and exist[] in increasing order.
2. Initialize a variable ans to store the minimum time to attend exactly one meeting.
3. Traverse the array and for each interval of the meetings, find the value which is just smaller than or equal to starti in the entrance[] array using upper_bound and find the value which is just greater than or equal to endiin the exist[] array using lower_bound.
4. Finally, print the minimum time to attend exactly one meeting.

Below is the implementation of the above approach:

## C++

 `// C++ program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to find the minimum time to``// attend exactly one meeting``int` `minTime(``int` `meeting[][2], ``int` `n,``          ``vector<``int``> entrance, ``int` `m,``             ``vector<``int``> &``exit``, ``int` `p)``{` `    ``// Stores minimum time to attend``    ``// exactly one meeting``    ``int` `ans = INT_MAX;` `    ``// Sort entrance[] array``    ``sort(entrance.begin(), entrance.end());` `    ``// Sort exit[] time``    ``sort(``exit``.begin(), ``exit``.end());` `    ``// Traverse meeting[][]``    ``for` `(``int` `i = 0; i < n; i++) {``        ` `        ``// Stores start time of``        ``// current meeting``        ``int` `u = meeting[i][0];``        ` `        ``// Stores end time of``        ``// current meeting``        ``int` `v = meeting[i][1];` `        ``// Find just greater value of``        ``// u in entrance[]``        ``auto` `it1``          ``= upper_bound(entrance.begin(),``                     ``entrance.end(), u);` `        ``// Find just greater or equal value``        ``//  of u in entrance[]``        ``auto` `it2``            ``= lower_bound(``exit``.begin(),``                         ``exit``.end(), v);` `        ``// Stores enter time to attend``        ``// the current meeting``        ``int` `start = it1``              ``- entrance.begin() - 1;``              ` `              ` `        ``// Stores exist time after``        ``// attending the meeting   ``        ``int` `end = it2 - ``exit``.begin();` `        ``// Update start lies in range [0, m -1]``        ``// and end lies in the range [0, p - 1]``        ``if` `(start >= 0 && start < m &&``                          ``end >= 0 && end < p)``                          ` `            ``// Update ans             ``            ``ans = min(ans,``                  ``exit``[end] - entrance[start]);``    ``}` `    ``// Return answer``    ``return` `ans >= INT_MAX ? -1 : ans;``}` `// Driver Code``int` `main()``{` `    ``// Stores interval of meeting``    ``int` `meeting[][2]``        ``= { { 15, 19 }, { 5, 10 }, { 7, 25 } };` `    ``// Stores entrance timings``    ``vector<``int``> entrance = { 4, 13, 25, 2 };` `    ``// Stores exit timings``    ``vector<``int``> ``exit` `= { 10, 25 };` `    ``// Stores total count of  meetings``    ``int` `n = (``sizeof``(meeting))``               ``/ ``sizeof``(meeting[0]);` `    ``// Stores total entrance timings``    ``int` `m = entrance.size();` `    ``// Stores total exit timings``    ``int` `p = ``exit``.size();` `    ``// Minimum time``    ``cout << minTime(meeting, n, entrance,``                               ``m, ``exit``, p)``         ``<< endl;``    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``import` `java.util.*;``class` `GFG{``  ` `static` `Vector exit =``       ``new` `Vector<>();``  ` `// Function to find the``// minimum time to attend``// exactly one meeting``static` `int` `minTime(``int` `meeting[][], ``int` `n,``                   ``int``[] entrance, ``int` `m,``                   ``int` `p)``{``  ``// Stores minimum time to attend``  ``// exactly one meeting``  ``int` `ans = Integer.MAX_VALUE;` `  ``// Sort entrance[] array``  ``Arrays.sort(entrance);` `  ``// Sort exit[] time``  ``Collections.sort(exit);` `  ``// Traverse meeting[][]``  ``for` `(``int` `i = ``0``; i < n; i++)``  ``{``    ``// Stores start time of``    ``// current meeting``    ``int` `u = meeting[i][``0``];` `    ``// Stores end time of``    ``// current meeting``    ``int` `v = meeting[i][``1``];` `    ``// Find just greater value of``    ``// u in entrance[]``    ``int` `it1 = upper_bound(entrance, ``0``,``                          ``entrance.length, u);` `    ``// Find just greater or equal``    ``// value of u in entrance[]``    ``int` `it2 = lowerBound(exit, ``0``,``                         ``exit.size(), v);``    ` `    ``// System.out.println(exit.size());``    ``// Stores enter time to attend``    ``// the current meeting``    ``int` `start = it1 - ``1`  `;` `    ``// Stores exist time after``    ``// attending the meeting   ``    ``int` `end = it2 ;` `    ``// Update start lies in range [0, m -1]``    ``// and end lies in the range [0, p - 1]``    ``if` `(start >= ``0` `&& start < m &&``        ``end >= ``0` `&& end < p)` `      ``// Update ans   ``      ``ans = Math.min(ans,``                     ``exit.get(end) -``                     ``entrance[start]);``  ``}` `  ``// Return answer``  ``return` `ans >= Integer.MAX_VALUE ?``         ``-``1` `: ans;``}` `static` `int` `upper_bound(``int``[] a, ``int` `low,``                       ``int` `high, ``int` `element)``{``  ``while``(low < high)``  ``{``    ``int` `middle = low +``                 ``(high - low) / ``2``;``    ` `    ``if``(a[middle] > element)``      ``high = middle;``    ``else``      ``low = middle + ``1``;``  ``}``  ``return` `low;``}` `static` `int` `lowerBound(Vector vec,``                      ``int` `low, ``int` `high,``                      ``int` `element)``{``  ``int` `[] array =``         ``new` `int``[vec.size()];``  ``int` `k = ``0``;``  ` `  ``for``(Integer val : vec)``  ``{``    ``array[k] = val;``    ``k++;``  ``}``  ` `  ``// vec.clear();``  ``while` `(low < high)``  ``{``    ``int` `middle = low +``                 ``(high - low) / ``2``;``    ` `    ``if` `(element > array[middle])``    ``{``      ``low = middle + ``1``;``    ``} ``else``    ``{``      ``high = middle;``    ``}``  ``}``  ``return` `low;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``  ``// Stores interval of meeting``  ``int` `meeting[][] = {{``15``, ``19``},``                     ``{``5``, ``10``},``                     ``{``7``, ``25``}};` `  ``// Stores entrance timings``  ``int` `[]entrance = {``4``, ``13``, ``25``, ``2``};` `  ``// Stores exit timings``  ``exit.add(``10``);``  ``exit.add(``25``);` `  ``// Stores total count of``  ``// meetings``  ``int` `n = meeting.length;` `  ``// Stores total entrance``  ``// timings``  ``int` `m = entrance.length;` `  ``// Stores total exit``  ``// timings``  ``int` `p = exit.size();``  ` `  ``// Minimum time``  ``System.out.print(minTime(meeting, n,``                           ``entrance, m,``                           ``p) + ``"\n"``);``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program to implement``# the above approach``from` `bisect ``import` `bisect_left, bisect_right``import` `sys` `# Function to find the minimum time to``# attend exactly one meeting``def` `minTime(meeting, n, entrance, m, exit, p):``    ` `    ``# Stores minimum time to attend``    ``# exactly one meeting``    ``ans ``=` `sys.maxsize` `    ``# Sort entrance[] array``    ``entrance ``=` `sorted``(entrance)` `    ``# Sort exit[] time``    ``exit ``=` `sorted``(exit)``    ` `    ``# Traverse meeting[][]``    ``for` `i ``in` `range``(n):``        ` `        ``# Stores start time of``        ``# current meeting``        ``u ``=` `meeting[i][``0``]``        ` `        ``# Stores end time of``        ``# current meeting``        ``v ``=` `meeting[i][``1``]` `        ``# Find just greater value of``        ``# u in entrance[]``        ``it1 ``=` `bisect_right(entrance, u)``        ` `        ``# Find just greater or equal value``        ``# of u in entrance[]``        ``it2 ``=` `bisect_left(exit, v)``        ` `        ``# Stores enter time to attend``        ``# the current meeting``        ``start ``=` `it1 ``-` `1``        ` `        ``# Stores exist time after``        ``# attending the meeting``        ``end ``=` `it2``        ` `        ``# Update start lies in range [0, m -1]``        ``# and end lies in the range [0, p - 1]``        ``if` `(start >``=` `0` `and` `start < m ``and``              ``end >``=` `0` `and` `end < p):``                  ` `            ``# Update ans``            ``ans ``=` `min``(ans, exit[end] ``-``                       ``entrance[start])``                       ` `    ``if` `ans >``=` `sys.maxsize:``        ``ans ``=` `-``1``        ` `    ``# Return answer``    ``return` `ans``    ` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Stores interval of meeting``    ``meeting ``=` `[ [ ``15``, ``19` `], [ ``5``, ``10` `], [ ``7``, ``25` `] ]``    ` `    ``# Stores entrance timings``    ``entrance ``=` `[ ``4``, ``13``, ``25``, ``2` `]``    ` `    ``# Stores exit timings``    ``exit ``=` `[ ``10``, ``25` `]``    ` `    ``# Stores total count of  meetings``    ``n ``=` `len``(meeting)` `    ``# Stores total entrance timings``    ``m ``=` `len``(entrance)` `    ``# Stores total exit timings``    ``p ``=` `len``(exit)` `    ``# Minimum time``    ``print``(minTime(meeting, n, entrance,``                  ``m, exit, p))` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program to implement``// the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{``  ` `static` `List<``int``> exit = ``new` `List<``int``>();``  ` `// Function to find the``// minimum time to attend``// exactly one meeting``static` `int` `minTime(``int` `[,]meeting, ``int` `n,``                   ``int``[] entrance, ``int` `m,``                   ``int` `p)``{``  ` `  ``// Stores minimum time to attend``  ``// exactly one meeting``  ``int` `ans = ``int``.MaxValue;` `  ``// Sort entrance[] array``  ``Array.Sort(entrance);` `  ``// Sort exit[] time``  ``exit.Sort();` `  ``// Traverse meeting[,]``  ``for``(``int` `i = 0; i < n; i++)``  ``{``    ` `    ``// Stores start time of``    ``// current meeting``    ``int` `u = meeting[i, 0];` `    ``// Stores end time of``    ``// current meeting``    ``int` `v = meeting[i, 1];` `    ``// Find just greater value of``    ``// u in entrance[]``    ``int` `it1 = upper_bound(entrance, 0,``                          ``entrance.Length, u);` `    ``// Find just greater or equal``    ``// value of u in entrance[]``    ``int` `it2 = lowerBound(exit, 0,``                         ``exit.Count, v);``    ` `    ``// Console.WriteLine(exit.Count);``    ``// Stores enter time to attend``    ``// the current meeting``    ``int` `start = it1 - 1;` `    ``// Stores exist time after``    ``// attending the meeting   ``    ``int` `end = it2;` `    ``// Update start lies in range [0, m -1]``    ``// and end lies in the range [0, p - 1]``    ``if` `(start >= 0 && start < m &&``          ``end >= 0 && end < p)` `      ``// Update ans   ``      ``ans = Math.Min(ans,``                     ``exit[end] -``                     ``entrance[start]);``  ``}` `  ``// Return answer``  ``return` `ans >= ``int``.MaxValue ?``          ``-1 : ans;``}` `static` `int` `upper_bound(``int``[] a, ``int` `low,``                       ``int` `high, ``int` `element)``{``  ``while` `(low < high)``  ``{``    ``int` `middle = low + (high - low) / 2;``    ` `    ``if` `(a[middle] > element)``      ``high = middle;``    ``else``      ``low = middle + 1;``  ``}``  ``return` `low;``}` `static` `int` `lowerBound(List<``int``> vec,``                      ``int` `low, ``int` `high,``                      ``int` `element)``{``  ``int` `[] array = ``new` `int``[vec.Count];``  ``int` `k = 0;``  ` `  ``foreach``(``int` `val ``in` `vec)``  ``{``    ``array[k] = val;``    ``k++;``  ``}``  ` `  ``// vec.Clear();``  ``while` `(low < high)``  ``{``    ``int` `middle = low + (high - low) / 2;``    ` `    ``if` `(element > array[middle])``    ``{``      ``low = middle + 1;``    ``}``    ``else``    ``{``      ``high = middle;``    ``}``  ``}``  ``return` `low;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``  ` `  ``// Stores interval of meeting``  ``int` `[,]meeting = { { 15, 19 },``                     ``{ 5, 10 },``                     ``{ 7, 25 } };` `  ``// Stores entrance timings``  ``int` `[]entrance = { 4, 13, 25, 2 };``  ` `  ``// Stores exit timings``  ``exit.Add(10);``  ``exit.Add(25);` `  ``// Stores total count of``  ``// meetings``  ``int` `n = meeting.GetLength(0);` `  ``// Stores total entrance``  ``// timings``  ``int` `m = entrance.Length;` `  ``// Stores total exit``  ``// timings``  ``int` `p = exit.Count;``  ` `  ``// Minimum time``  ``Console.Write(minTime(meeting, n,``                        ``entrance, m,``                        ``p) + ``"\n"``);``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``
Output:
`6`

Time Complexity: O(N * max(logP, logM)) where M and P are the lengths of entrance[] and exist[] arrays.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up