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