Minimum time required to transport all the boxes from source to the destination under the given constraints | Function that returns true if it is possible to transport all the boxes in the given amount of time ; If all the boxes can be transported in the given time ; If all the boxes can 't be  transported in the given time ; Function to return the minimum time required ; Sort the two arrays ; Stores minimum time in which all the boxes can be transported ; Check for the minimum time in which all the boxes can be transported ; If it is possible to transport all the boxes in mid amount of time ; Driver code
< ? php function isPossible ( $ box , $ truck , $ n , $ m , $ min_time ) { $ temp = 0 ; $ count = 0 ; while ( $ count < $ m ) { for ( $ j = 0 ; $ j < $ min_time && $ temp < $ n && $ truck [ $ count ] >= $ box [ $ temp ] ; $ j += 2 ) $ temp ++ ; $ count ++ ; } if ( $ temp == $ n ) return true ; return false ; } function minTime ( $ box , $ truck , $ n , $ m ) { sort ( $ box ) ; sort ( $ truck ) ; $ l = 0 ; $ h = 2 * $ n ; $ min_time = 0 ; while ( $ l <= $ h ) { $ mid = intdiv ( ( $ l + $ h ) , 2 ) ; if ( isPossible ( $ box , $ truck , $ n , $ m , $ mid ) ) { $ min_time = $ mid ; $ h = $ mid - 1 ; } else $ l = $ mid + 1 ; } return $ min_time ; } $ box = array ( 10 , 2 , 16 , 19 ) ; $ truck = array ( 29 , 25 ) ; $ n = sizeof ( $ box ) ; $ m = sizeof ( $ truck ) ; echo minTime ( $ box , $ truck , $ n , $ m ) ; ? >