How to come up with this solution
Thought Process
As we have to find the max/min in a range, binary search on answer should be the correct approach.
Since binary search on answer is computed ove
## Thought Process
- As we have to find the max/min in a range, binary search on answer should be the correct approach.
- Since binary search on answer is computed over a given range, we should take the range to be from `1` to `1e18` because the return type is `long long`. If the return type were `int`, then we could take the range from `1` to `1e9`.
- We take the mid value as `low + (high - low) / 2` to avoid the risk of overflow.
- Then, we iterate and calculate the `carsRepaired` on the current `mid` value.
- Once the number of cars repaired is less than the required number of cars, we adjust the low pointer (or shrink the window from the left) to get the optimal value where the `carsRepaired` are greater than or equal to the number of cars we need to repair.

![image.png](https://assets.leetcode.com/users/images/5f83d5dc-25ef-4097-a121-f3e2bb9dc34f_1742120298.0131574.png)


---
## Implementation
```cpp []
class Solution {
public:
    long long repairCars(vector<int>& ranks, int cars) {
        long long low = 1 , high = 1e18 , n = ranks.size(); 
        while (low < high) {
            long long mid = low + (high - low)/2 , carsRepaired = 0;

            for (int i = 0 ; i < n; i++) 
            carsRepaired += sqrt(mid/ranks[i]);

            if (carsRepaired >= cars) high = mid;
            else low = mid + 1;
        }
        return low;
    }
};
```
```Python []
class Solution:
    def repairCars(self, ranks: List[int], cars: int) -> int:
        low, high = 1, 10**18
        while low < high:
            mid = low + (high - low) // 2
            carsRepaired = 0
            for r in ranks:
                carsRepaired += math.isqrt(mid // r)
            if carsRepaired >= cars:
                high = mid
            else:
                low = mid + 1
        return low
```
```Java []
class Solution {
    public long repairCars(int[] ranks, int cars) {
        long low = 1, high = (long) 1e18;
        while (low < high) {
            long mid = low + (high - low) / 2;
            long carsRepaired = 0;
            for (int r : ranks) {
                carsRepaired += (long) Math.floor(Math.sqrt(mid / (double) r));
            }
            if (carsRepaired >= cars) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        return low;
    }
}
```
```Go []
func repairCars(ranks []int, cars int) int64 {
    low := int64(1)
    high := int64(1e18)
    for low < high {
        mid := low + (high - low) / 2
        var carsRepaired int64 = 0
        for _, r := range ranks {
            carsRepaired += int64(math.Floor(math.Sqrt(float64(mid) / float64(r))))
        }
        if carsRepaired >= int64(cars) {
            high = mid
        } else {
            low = mid + 1
        }
    }
    return low
}
```
['Array', 'Binary Search', 'Python', 'C++', 'Java', 'Go']