Length of smallest meeting that can be attended | Javascript 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 [ ] ; System . out . println ( exit . size ( ) ) ; 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 ( ) ; ; 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
let exit = [ ] ; function minTime ( meeting , n , entrance , m , p ) { let ans = Number . MAX_VALUE ; ( entrance ) . sort ( function ( a , b ) { return a - b ; } ) ; ( exit ) . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n ; i ++ ) { let u = meeting [ i ] [ 0 ] ; let v = meeting [ i ] [ 1 ] ; let it1 = upper_bound ( entrance , 0 , entrance . length , u ) ; let it2 = lowerBound ( exit , 0 , exit . length , v ) ; let start = it1 - 1 ; let end = it2 ; if ( start >= 0 && start < m && end >= 0 && end < p ) ans = Math . min ( ans , exit [ end ] - entrance [ start ] ) ; } return ans >= Number . MAX_VALUE ? - 1 : ans ; } function upper_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } function lowerBound ( vec , low , high , element ) { let array = new Array ( vec . length ) ; let k = 0 ; for ( let val = 0 ; val < vec . length ; val ++ ) { array [ k ] = vec [ val ] ; k ++ ; } while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( element > array [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low ; } let meeting = [ [ 15 , 19 ] , [ 5 , 10 ] , [ 7 , 25 ] ] ; let entrance = [ 4 , 13 , 25 , 2 ] ; exit . push ( 10 ) ; exit . push ( 25 ) ; let n = meeting . length ; let m = entrance . length ; let p = exit . length ; document . write ( minTime ( meeting , n , entrance , m , p ) + " " ) ;