Binary Search - 2

Covered in this section:

1. Factory Machines (CSES)

The code solves the problem of finding the minimum time required to produce t products using n machines that work in parallel, each taking a certain time to make one product. It uses binary search over time: for a time [mid], it calculates how many products all machines can collectively produce [summation of (mid / v[i] per machine)]. If they can produce t or more products, it means the time is sufficient, so it tries to find a smaller possible time by updating the upper bound. Otherwise, it increases the lower bound. This process continues until the smallest such valid time (ans) is found, ensuring optimal efficiency.

Since t has a range of [1, 1e9], we set our high as 1e9 and low as 0

Caution: when calculating total Product produced for given time, the total might overflow therefore it is adviced to break total product summation loop when it exceed t.

2. Search-in-Rotated-array (Leetcode)

Example of a rotated array:

[4, 5, 6, 7, 1, 2, 3]

Intuition behind this problem: Binary search is not only about selecting indexs/values that satisfy given conditions but also about eliminating indexs that dont. Indexes that are above high and below low are "eliminated" Indexes. Therefore Try to eliminate either the right half or the left half of the given array to slim down your options.

Now the question, how do you eliminate parts of the array? We know binary Search can only be applied to a sorted array. From Mid value wither the right half will be sorted or the left half. Depending on that adn the value of [mid] we need to decide which part of array we need to filter out.

Here is a slightly more complicated version of the same problem

Hint: the only condition that is not accounted for in the previous problem compared to this problem is: nums[low] = nums[mid] = nums[high]