


The first line contains two integers, ‘n’ and ‘d’, denoting the size of the array ‘weights’ and the maximum weight capacity of the ship.
The second line contains ‘n’ integers denoting the elements of the array ‘weights’.
Return the least weight capacity so that you can ship all the packages within 'd' days.
You are not required to print the output explicitly, it has already been taken care of. Just implement the function.
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:
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 'minShipCapacity' to 'maxShipCapacity', 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 the least weight capacity can lie. Then use a while loop to find the answer.