Binary Search ? yeah| C++
IntuitionWe are given several mechanics, each with a different efficiency (represented by their "rank"). A mechanic with rank r takes r * t^2 time to fix t cars
# Intuition
We are given several mechanics, each with a different efficiency (represented by their "rank"). A mechanic with rank `r` takes `r * t^2` time to fix `t` cars. Our goal is to **assign cars across mechanics** in such a way that **all cars are repaired in the minimum total time**.

We can’t assign cars greedily since one bad decision might block better ones later. So, we use **Binary Search on Time** to find the minimum possible time `T` such that all cars can be fixed.

# Approach
- **Binary Search** on time between `1` and `maxRank * cars^2` (worst case: slowest mechanic fixes all cars).
- For each mid time, check if it's **possible to fix all cars** within that time using the `isPossible` function.
  - Each mechanic can fix at most `floor(sqrt(T / rank))` cars within time `T`.
  - Sum over all mechanics and compare with required number of cars.
- If it’s possible, try a smaller time (move left); else, increase time (move right).

# Complexity
- **Time complexity:**  
  - Binary Search runs in $O(log(maxRank * cars^2))$
  - For each mid value, we iterate over all mechanics: $O(n)$
  - Total: **$O(n * log(maxRank * cars^2))$**

- **Space complexity:**  
  - We use only a few variables and no extra data structures.  
  - Total: $O(1)$

---

# Code
```cpp
class Solution {
public:
    typedef long long ll;

    bool isPossible(vector<int>& ranks, ll mid, int cars) {
        ll carsFixed = 0;

        for(int i = 0; i < ranks.size(); i++) {
            carsFixed += 1LL * sqrt(mid / ranks[i]);  // max cars a mechanic can fix
            if (carsFixed >= cars) return true; // early exit
        }

        return carsFixed >= cars;
    }

    long long repairCars(vector<int>& ranks, int cars) {
        ll l = 1;
        int minR = *min_element(ranks.begin(), ranks.end());
        ll r = 1LL * minR * cars * cars;

        ll result = -1;
        while (l <= r) {
            ll mid = l + (r - l) / 2;

            if (isPossible(ranks, mid, cars)) {
                result = mid;
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }

        return result;
    }
};

```
![Upvote3.png](https://assets.leetcode.com/users/images/1a402111-c552-4af2-84d7-d4c9b8e09a4b_1742663588.6502573.png)

['Array', 'Binary Search', 'C++']