Skip to content

Taking it to the next level

Course: Binary search, two sum and strings - chapter 7

We discussed earlier, that if we have some monotonic array, where each element satisfies a monotonic property, we can binary search on the monotonic property using 0-1 array.

But, this holds true even for a non-monotonic array. In general, if we have some property that is monotonic, we can binary search for it.

We saw that with the peak element example, the array was not monotonic. But we identified a property arr[i] > arr[i-1] that is monotonic, and based on that did a binary search. However, often the property that makes it monotonic is not so simple.

Let’s understand with an example: https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/description/

Here the weights in not a monotonic array (i.e. neither increasing nor decreasing).

Let’s say the maximum capacity of the ship is X.

Now for X, if it takes more than D days, obviously if the capacity is < X, then also it takes more than D days. (Think like this, if the ship can carry max capacity of 10 kgs and with this it takes 5 days, then if it has max capacity of 5 kgs, obviously it will take ≥ 5 days)

Similarly, if the ship takes ≤D days with X capacity, it will obviously take ≤D days when its capacity is > X. (Example, if ship can carry 10kgs and it takes 5 days, obviously it will take ≤ 5 days if its capacity was 15 kgs)

So the property maximum capacity of the ship is itself monotonic with the number of days taken.

As max capacity increases, the days taken decrease. As capacity decreases, days taken increase.

Let’s say G(x) returns the number of days taken when max capacity is x.

F(x) is whether the ship can transport all the goods within D days if its max capacity is x.

That is, F(x) = G(x) <= D

If we carefully observe, F(x) is monotonic.

For lower values of x, G(x) is more and thus F(x) is false. As x increases, a point comes when G(x) ≤ D, and F(x) becomes true. And from there, it always stays true as x increases.

Now, what do we want. Minimum possible value of the maximum capacity. So we want the first x, where F(x) becomes true. 🙂

That’s it!

The code becomes something like this

int lo = LOW_VAL, hi = MAX_VAL;
while (lo < hi-1) {
int mid = (lo + hi)/2;
if (F(mid)) hi = mid;
else lo = mid;
}
return hi;

Now what is pending to identify? LOW_VAL, HIGH_VAL and how is G(x) calculated

LOW_VAL = Lowest possible value of maximum capacity. That is the maximum capacity cannot be lower than this. So in the worst case, we need to carry all the packages right? And one package / day. So the ship should be at least able to carry the maximum weighted package.

LOW_VAL = *max_element(weights.begin(), weights.end());

MAX_VAL = Maximum possible capacity that is needed for our problem. In best case, the ship transfers all the goods in one day itself. So at max, it needs a total capacity of total sum of all the weights.

MAX_VAL = accumulate(weights.begin(), weights.end(), 0);

Finally G(x). What is it? Given max carrying capacity of the ship is x, how many days would it take. We can just run a linear search for this because it has to transfer all the weights in order.

int G(int x, vector<int> &weights) {
int days = 0;
int current_weight = 0;
for(int i=0; i<weights.size(); i++) {
if (current_weight + weights[i] <= x) {
// We can take this in the same day
current_weight += weights[i];
} else {
// We need to take this on a new day
days++;
current_weight = weights[i];
}
}
return days;
}
bool F(int x, int d, vector<int> &weights) {
return G(x, weights) <= d;
}

Now you might be wondering, wouldn’t it take a lot of time, or not meet the time constraints?

Let’s analyze the complexity. We are running a binary search. For each binary search iteration, we are calling F(mid) which in turn calls G(mid). So each iteration takes O(N) time. But how many iterations in binary search? It’s log2(N).

So overall complexity is: O(Nlog2(N))

💡 Generally, if you come across problems like minimizing the maximum value of some property or maximizing the minimum value of some property, or minimization / maximization in general, binary search can be one of the ways to solve it.