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
function isPossible ( box , truck , n , m , min_time ) { let temp = 0 ; let count = 0 ; while ( count < m ) { for ( let 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 ) { box . sort ( function ( a , b ) { return a - b } ) ; truck . sort ( function ( a , b ) { return a - b } ) ; let l = 0 ; let h = 2 * n ; let min_time = 0 ; while ( l <= h ) { let mid = Math . floor ( ( l + h ) / 2 ) ; if ( isPossible ( box , truck , n , m , mid ) ) { min_time = mid ; h = mid - 1 ; } else l = mid + 1 ; } return min_time ; } let box = [ 10 , 2 , 16 , 19 ] ; let truck = [ 29 , 25 ] ; let n = box . length ; let m = truck . length ; document . write ( minTime ( box , truck , n , m ) ) ;