Length of smallest meeting that can be attended | Java 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 ( ) ; ; 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
import java . util . * ; class GFG { static Vector < Integer > exit = new Vector < > ( ) ; static int minTime ( int meeting [ ] [ ] , int n , int [ ] entrance , int m , int p ) { int ans = Integer . MAX_VALUE ; Arrays . sort ( entrance ) ; Collections . sort ( exit ) ; 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 . size ( ) , v ) ; int start = it1 - 1 ; int end = it2 ; if ( start >= 0 && start < m && end >= 0 && end < p ) ans = Math . min ( ans , exit . get ( end ) - entrance [ start ] ) ; } 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 < Integer > vec , int low , int high , int element ) { int [ ] array = new int [ vec . size ( ) ] ; int k = 0 ; for ( Integer val : 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 . length ; int m = entrance . length ; int p = exit . size ( ) ; System . out . print ( minTime ( meeting , n , entrance , m , p ) + "NEW_LINE"); } }