# Capacity To Ship Packages Within D Days

#### You are the owner of a Shipment company. You use conveyor belts to ship packages from one port to another. The packages must be shipped within 'D' days.

#### The weights of the packages are given in an array 'WEIGHTS'. The packages are loaded on the conveyor belts every day in the same order as they appear in the array. The loaded weights must not exceed the maximum weight capacity of the ship.

#### Find out the least weight capacity so that you can ship all the packages within 'D' days.

##### Input Format:

```
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line of each test case contains two integers, ‘N’ and ‘D’, denoting the size of the array ‘WEIGHTS’ and the maximum weight capacity of the ship.
The second line of each test case contains ‘N’ integers denoting the elements of the array ‘WEIGHTS’.
```

##### Output Format:

```
For each test case, return the least weight capacity so that you can ship all the packages within 'D' days.
```

##### Note:

```
You are not required to print the output explicitly, it has already been taken care of. Just implement the function.
```

##### Constraints:

```
1<= T <= 10
1 <= N <= 50000
1 <= D <= 50000
1 <= WEIGHTS[i] <= 500
Time Limit: 1 sec
```

The idea is to find the range in which the weight capacity of the ship can lie and then iterate in the given range and find the smallest weight capacity such that we can ship all the packages within ‘D’ days. The minimum value of the ship capacity is the maximum weight in the given array 'WEIGHTS', and the maximum value of the ship capacity is the sum of all the weights of the array 'WEIGHTS'.

The steps are as follows:

- Initialize 'MIN_SHIP_CAPACITY' = maximum value of the array 'WEIGHTS' and 'MAX_SHIP_CAPACITY' = sum of all the values of array 'WEIGHTS'.
- Iterate from i = 'MIN_SHIP_CAPACITY' to 'MAX_SHIP_CAPACITY':
- Check if the given value of i is taken as the least weight capacity then Will it be possible to ship all the packages within D days?
- Define a function 'CHECK_VALIDITY' which takes three arguments ‘capacity’, 'WEIGHTS', and ‘D’, which denotes the current weight capacity for which we are checking, the given array 'WEIGHTS', and the given integer ‘D’, respectively.
- Initialize two variables, ‘CURRENT_DAY’ equal to 1 and 'CURRENT_DAY_WEIGHT' to 0, which denotes the number of the day for which we are shipping the packages, and the total weight that can be carried on the Kth Day, respectively.
- Iterate over the array 'WEIGHTS':
- Check if, after adding the current element, the total weight for the current day exceeds the weight capacity of the ship or not.
- If it doesn’t exceed, then add the current weight to CURRENT_DAYWeight.
- If it exceeds, then increase the required number of days by incrementing the ‘CURRENT_DAY’ and then set the value of 'CURRENT_DAY_WEIGHT' to the current element.

- Check if ‘CURRENT_DAY’ becomes greater than ‘D’. If it becomes greater than ‘D’, it means packages can’t be delivered in the expected number of days. Return false.
- Return true as the packages can be delivered within ‘D’ days.
- If 'CURRENT_VALIDITY' returns true, then return i as it is the least answer.

The idea is to use binary search instead of linear search. The reason being Binary search can be applied on a sorted range, and in this case, from 'MIN_SHIP_CAPACITY' to 'MAX_SHIP_CAPACITY', it is also sorted range. So, if we can use binary search, then we can cut down the time complexity from O(sum) to O(log(sum)) for searching, which increases the efficiency.

From the previous approach, we already found out the range in which least weight capacity can lie. Then use a while loop for finding the answer.

- Initialize 'MIN_SHIP_CAPACITY' = maximum value of the array 'WEIGHTS' and 'MAX_SHIP_CAPACITY' = sum of all the values of array 'WEIGHTS'.
- Execute a while loop with the condition that 'MAX_SHIP_CAPACITY' >= 'MIN_SHIP_CAPACITY':

- Initialize capacity = ('MIN_SHIP_CAPACITY' + 'MAX_SHIP_CAPACITY')/2;
- Check if the given value of i is taken as the least weight capacity then Will it be possible to ship all the packages within D days?
- Define a function ‘CHECK_VALIDITY’ which takes three arguments ‘capacity’, 'WEIGHTS', and ‘D’, which denotes the current weight capacity for which we are checking, the given array 'WEIGHTS', and the given integer ‘D’ respectively.
- Initialize two variables, 'CURRENT_DAY' equal to 1 and 'CURRENT_DAY_WEIGHT' to 0, which denotes the number of the day for which we are shipping the packages, and the total weight that can be carried on the Kth Day, respectively.
- Iterate over the array 'WEIGHTS':
- Check if, after adding the current element, the total weight for the current day exceeds the weight capacity of the ship or not.
- If it doesn’t exceed, then add the current weight to currentDayWeight.
- If it exceeds, then increase the required number of days by incrementing the ‘currentDaycurrentDay’ and then set the value of 'CURRENT_DAY_WEIGHT' to the current element.

- Check if ‘CURRENT_DAY’ becomes greater than ‘D’. If it becomes greater than ‘D’, it means packages can’t be delivered in the expected number of days. Return false.
- Return true as the packages can be delivered within ‘D’ days.

- If it’s valid, then change the value of 'MAX_SHIP_CAPACITY' to capacity - 1.
- If not, then change the value of 'MIN_SHIP_CAPACITY' to capacity + 1.
- Return ‘'MIN_SHIP_CAPACITY'’ as the final answer. The idea behind this step is that we are storing the minimum of all the possible values of the least weight capacity, i.e., whenever we encounter any possible value which is less than the current value of l, then we update it.