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 [ ] ; Console . WriteLine ( exit . Count ) ; 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 ; vec . Clear ( ) ; ; 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
using System ; using System . Collections . Generic ; class GFG { static List < int > exit = new List < int > ( ) ; static int minTime ( int [ , ] meeting , int n , int [ ] entrance , int m , int p ) { int ans = int . MaxValue ; Array . Sort ( entrance ) ; exit . Sort ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int u = meeting [ i , 0 ] ; int v = meeting [ i , 1 ] ; int it1 = upper_bound ( entrance , 0 , entrance . Length , u ) ; int it2 = lowerBound ( exit , 0 , exit . Count , v ) ; int start = it1 - 1 ; int end = it2 ; if ( start >= 0 && start < m && end >= 0 && end < p ) ans = Math . Min ( ans , exit [ end ] - entrance [ start ] ) ; } 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 ++ ; } while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > array [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low ; } public static void Main ( String [ ] args ) { int [ , ] meeting = { { 15 , 19 } , { 5 , 10 } , { 7 , 25 } } ; int [ ] entrance = { 4 , 13 , 25 , 2 } ; exit . Add ( 10 ) ; exit . Add ( 25 ) ; int n = meeting . GetLength ( 0 ) ; int m = entrance . Length ; int p = exit . Count ; Console . Write ( minTime ( meeting , n , entrance , m , p ) + " STRNEWLINE " ) ; } }