Minimum time required to transport all the boxes from source to the destination under the given constraints | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int box [ ] , int truck [ ] , int n , int m , int min_time ) { int temp = 0 ; int count = 0 ; while ( count < m ) { for ( int j = 0 ; j < min_time && temp < n && truck [ count ] >= box [ temp ] ; j += 2 ) temp ++ ; count ++ ; } if ( temp == n ) return true ; return false ; } int minTime ( int box [ ] , int truck [ ] , int n , int m ) { sort ( box , box + n ) ; sort ( truck , truck + m ) ; int l = 0 ; int h = 2 * n ; int min_time = 0 ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( isPossible ( box , truck , n , m , mid ) ) { min_time = mid ; h = mid - 1 ; } else l = mid + 1 ; } return min_time ; } int main ( ) { int box [ ] = { 10 , 2 , 16 , 19 } ; int truck [ ] = { 29 , 25 } ; int n = sizeof ( box ) / sizeof ( int ) ; int m = sizeof ( truck ) / sizeof ( int ) ; printf ( " % d " , minTime ( box , truck , n , m ) ) ; return 0 ; }