Related Articles
Minimum time required to transport all the boxes from source to the destination under the given constraints
• Difficulty Level : Hard
• Last Updated : 20 May, 2019

Given two arrays box[] and truck[] where box[i] represents the weight of the ith box and truck[i] represents the maximum load that the ith truck can carry. Now each truck takes 1 hour to transport a box from source to destination and another one hour to come back. Now given that all the boxes are kept at the source, the task is to find the minimum time required to transport all the boxes from source to the destination. Note that there will always be some time in which the boxes can be transported and only a single box can be carried by a truck at any instance of time.

Examples:

Input: box[] = {7, 6, 5, 4, 3}, truck[] = {10, 3}
Output: 7
1st hour: truck carries box and truck carries box
2nd hour: Both truck are back on the source location.
Now, truck cannot carry anymore boxes as all the remaining boxes
have weights more than the capacity of truck.
So, truck will carry box and box
in total of four hours. (source-destination and then destination-source)
And finally box will take another hour to reach the destination.
So, total time taken = 2 + 4 + 1 = 7

Input: box[] = {10, 2, 16, 19}, truck[] = {29, 25}
Output: 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use binary search and sort the two arrays, here the lower bound will be 0 and upper bound will be 2 * size of box[] because in the worst case the amount of time required to transport all the boxes will be 2 * size of box array, now compute the mid value, and for each mid value check if all the boxes can be transported by the loaders in time = mid. If yes, then update upper bound as mid – 1 and if no then update lower bound as mid + 1.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;`` ` `// Function that returns true if it is``// possible to transport all the boxes``// in the given amount of time``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 all the boxes can be``    ``// transported in the given time``    ``if` `(temp == n)``        ``return` `true``;`` ` `    ``// If all the boxes can't be``    ``// transported in the given time``    ``return` `false``;``}`` ` `// Function to return the minimum time required``int` `minTime(``int` `box[], ``int` `truck[], ``int` `n, ``int` `m)``{`` ` `    ``// Sort the two arrays``    ``sort(box, box + n);``    ``sort(truck, truck + m);`` ` `    ``int` `l = 0;``    ``int` `h = 2 * n;`` ` `    ``// Stores minimum time in which``    ``// all the boxes can be transported``    ``int` `min_time = 0;`` ` `    ``// Check for the minimum time in which``    ``// all the boxes can be transported``    ``while` `(l <= h) {``        ``int` `mid = (l + h) / 2;`` ` `        ``// If it is possible to transport all``        ``// the boxes in mid amount of time``        ``if` `(isPossible(box, truck, n, m, mid)) {``            ``min_time = mid;``            ``h = mid - 1;``        ``}``        ``else``            ``l = mid + 1;``    ``}`` ` `    ``return` `min_time;``}`` ` `// Driver code``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;``}`

## Java

 `// Java implementation of the approach``import` `java.util.Arrays;`` ` `class` `GFG``{`` ` `// Function that returns true if it is``// possible to transport all the boxes``// in the given amount of time``static` `boolean` `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 all the boxes can be``    ``// transported in the given time``    ``if` `(temp == n)``        ``return` `true``;`` ` `    ``// If all the boxes can't be``    ``// transported in the given time``    ``return` `false``;``}`` ` `// Function to return the minimum time required``static` `int` `minTime(``int` `box[], ``int` `truck[], ``int` `n, ``int` `m)``{`` ` `    ``// Sort the two arrays``    ``Arrays.sort(box);``    ``Arrays.sort(truck);`` ` `    ``int` `l = ``0``;``    ``int` `h = ``2` `* n;`` ` `    ``// Stores minimum time in which``    ``// all the boxes can be transported``    ``int` `min_time = ``0``;`` ` `    ``// Check for the minimum time in which``    ``// all the boxes can be transported``    ``while` `(l <= h) {``        ``int` `mid = (l + h) / ``2``;`` ` `        ``// If it is possible to transport all``        ``// the boxes in mid amount of time``        ``if` `(isPossible(box, truck, n, m, mid))``        ``{``            ``min_time = mid;``            ``h = mid - ``1``;``        ``}``        ``else``            ``l = mid + ``1``;``    ``}`` ` `    ``return` `min_time;``}`` ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `box[] = { ``10``, ``2``, ``16``, ``19` `};``    ``int` `truck[] = { ``29``, ``25` `};`` ` `    ``int` `n = box.length;``    ``int` `m = truck.length;`` ` `    ``System.out.printf(``"%d"``, minTime(box, truck, n, m));``}``}`` ` `/* This code contributed by PrinciRaj1992 */`

## Python3

 `# Python3 implementation of the approach `` ` `# Function that returns true if it is ``# possible to transport all the boxes ``# in the given amount of time ``def` `isPossible(box, truck, n, m, min_time) :``     ` `    ``temp ``=` `0``    ``count ``=` `0`` ` `    ``while` `(count < m) :``        ``j ``=` `0``        ``while` `(j < min_time ``and` `temp < n ``and` `                    ``truck[count] >``=` `box[temp] ):``            ``temp ``+``=``1``            ``j ``+``=` `2`` ` `        ``count ``+``=` `1`` ` `    ``# If all the boxes can be ``    ``# transported in the given time ``    ``if` `(temp ``=``=` `n) :``        ``return` `True`` ` `    ``# If all the boxes can't be ``    ``# transported in the given time ``    ``return` `False`` ` `# Function to return the minimum time required ``def` `minTime(box, truck, n, m) : `` ` `    ``# Sort the two arrays ``    ``box.sort(); ``    ``truck.sort(); `` ` `    ``l ``=` `0``    ``h ``=` `2` `*` `n `` ` `    ``# Stores minimum time in which ``    ``# all the boxes can be transported ``    ``min_time ``=` `0`` ` `    ``# Check for the minimum time in which ``    ``# all the boxes can be transported ``    ``while` `(l <``=` `h) :``        ``mid ``=` `(l ``+` `h) ``/``/` `2`` ` `        ``# If it is possible to transport all ``        ``# the boxes in mid amount of time ``        ``if` `(isPossible(box, truck, n, m, mid)) :``            ``min_time ``=` `mid ``            ``h ``=` `mid ``-` `1``     ` `        ``else` `:``             ` `            ``l ``=` `mid ``+` `1`` ` `    ``return` `min_time`` ` `# Driver code ``if` `__name__ ``=``=` `"__main__"` `: `` ` `    ``box ``=` `[ ``10``, ``2``, ``16``, ``19` `] ``    ``truck ``=` `[ ``29``, ``25` `] `` ` `    ``n ``=` `len``(box) ``    ``m ``=` `len``(truck)`` ` `    ``print``(minTime(box, truck, n, m))``     ` `# This code is contributed by Ryuga`

## C#

 `// C# implementation of the approach``using` `System;``     ` `class` `GFG``{`` ` `// Function that returns true if it is``// possible to transport all the boxes``// in the given amount of time``static` `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 all the boxes can be``    ``// transported in the given time``    ``if` `(temp == n)``        ``return` `true``;`` ` `    ``// If all the boxes can't be``    ``// transported in the given time``    ``return` `false``;``}`` ` `// Function to return the minimum time required``static` `int` `minTime(``int` `[]box, ``int` `[]truck, ``int` `n, ``int` `m)``{`` ` `    ``// Sort the two arrays``    ``Array.Sort(box);``    ``Array.Sort(truck);`` ` `    ``int` `l = 0;``    ``int` `h = 2 * n;`` ` `    ``// Stores minimum time in which``    ``// all the boxes can be transported``    ``int` `min_time = 0;`` ` `    ``// Check for the minimum time in which``    ``// all the boxes can be transported``    ``while` `(l <= h)``    ``{``        ``int` `mid = (l + h) / 2;`` ` `        ``// If it is possible to transport all``        ``// the boxes in mid amount of time``        ``if` `(isPossible(box, truck, n, m, mid))``        ``{``            ``min_time = mid;``            ``h = mid - 1;``        ``}``        ``else``            ``l = mid + 1;``    ``}`` ` `    ``return` `min_time;``}`` ` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``int``[] box = { 10, 2, 16, 19 };``    ``int` `[]truck = { 29, 25 };`` ` `    ``int` `n = box.Length;``    ``int` `m = truck.Length;`` ` `    ``Console.WriteLine(``"{0}"``, minTime(box, truck, n, m));``}``}`` ` `/* This code contributed by PrinciRaj1992 */`

## PHP

 `= ``\$box``[``\$temp``];``            ``\$j` `+= 2)``            ``\$temp``++;`` ` `        ``\$count``++;``    ``}`` ` `    ``// If all the boxes can be``    ``// transported in the given time``    ``if` `(``\$temp` `== ``\$n``)``        ``return` `true;`` ` `    ``// If all the boxes can't be``    ``// transported in the given time``    ``return` `false;``}`` ` `// Function to return the minimum time required``function` `minTime( ``\$box``, ``\$truck``, ``\$n``, ``\$m``)``{`` ` `    ``// Sort the two arrays``    ``sort(``\$box``);``    ``sort(``\$truck``);`` ` `    ``\$l` `= 0;``    ``\$h` `= 2 * ``\$n``;`` ` `    ``// Stores minimum time in which``    ``// all the boxes can be transported``    ``\$min_time` `= 0;`` ` `    ``// Check for the minimum time in which``    ``// all the boxes can be transported``    ``while` `(``\$l` `<= ``\$h``) {``        ``\$mid` `= intdiv((``\$l` `+ ``\$h``) , 2);`` ` `        ``// If it is possible to transport all``        ``// the boxes in mid amount of time``        ``if` `(isPossible(``\$box``, ``\$truck``, ``\$n``, ``\$m``, ``\$mid``)) ``        ``{``            ``\$min_time` `= ``\$mid``;``            ``\$h` `= ``\$mid` `- 1;``        ``}``        ``else``            ``\$l` `= ``\$mid` `+ 1;``    ``}`` ` `    ``return` `\$min_time``;``}`` ` `// Driver code``\$box` `= ``array``( 10, 2, 16, 19 );``\$truck` `= ``array``( 29, 25 );`` ` `\$n` `= sizeof(``\$box``);``\$m` `= sizeof(``\$truck``);`` ` `echo` `minTime(``\$box``, ``\$truck``, ``\$n``, ``\$m``);`` ` ` ` `// This code is contributed by ihritik`` ` `?>`
Output:
```3
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up