✅✅ Binary Search || Beginner Friendly || Easy Solution
Intuition
We have multiple mechanics with different ranks who are repairing cars. The time taken by a mechanic to repair a car increases quadratically based on
# Intuition
- We have multiple mechanics with different **ranks** who are repairing `cars`. The **time taken** by a mechanic to repair a car increases quadratically based on their rank.
- A mechanic with rank `r` takes **r × k²** time to repair `k` cars.
- We need to **minimize the total time** required to repair all cars by distributing the work optimally.

# Approach
### **1. Using Binary Search on Time**
- Instead of brute-force checking all possible ways to distribute cars, we apply **binary search on the minimum possible time** required.
- The **search space** for the time is:
  - **Lower bound (`l`)**: `1` (minimum time required).
  - **Upper bound (`r`)**: `max(ranks) * cars²` (worst case, where the slowest mechanic repairs all cars).

### **2. Checking Feasibility using `isPossible()`**
- We define `isPossible(mid)`, which checks if all `cars` can be repaired within `mid` time:
  - For each mechanic with rank `r`, the **maximum cars they can fix** within `mid` time is:
    `carsFixed = sqrt(mid*rank)`
  - If the sum of all `carsFixed` across mechanics is **at least `cars`**, then `mid` is a valid answer.

### **3. Binary Search Execution**
- Start with `l = 1` and `r = max(ranks) * cars²`.
- Compute `mid = (l + r) / 2` and check:
  - If `isPossible(mid) == true`, update `ans = mid` and move **left** (`r = mid - 1`).
  - Else, move **right** (`l = mid + 1`).
- Continue until `l > r`.

---

# **Time Complexity Analysis**
1. **Binary Search on Time:**  
   - The search space is `[1, max(ranks) * cars²]`, which has **O(log(maxRank * cars²))** iterations.
   
2. **Checking Feasibility (`isPossible`)**  
   - For each `mid`, we iterate over `ranks`, making it **O(n)**.
   
3. **Total Complexity:**  
   - **O(n log(maxRank * cars²))**, which is efficient.

# **Space Complexity**
- **O(1)** → We use only a few integer and long long variables.

# Summary
✅ Binary search on time for efficiency
✅ Uses sqrt(mid / rank) to distribute workload optimally
✅ O(n log(maxRank * cars²)) time complexity
✅ O(1) space complexity

# Code
```cpp []
class Solution {
public:
    typedef long long ll;
    bool isPossible(vector<int>&ranks,int &cars,ll &mid){
        ll carsFixed=0;
        for(int i=0;i<ranks.size();i++){
            carsFixed+=sqrt(mid/ranks[i]);
        }
        return carsFixed>=cars;
    }
    long long repairCars(vector<int>& ranks, int cars) {
        int n=ranks.size();
        ll l=1;
        ll maxWord=*max_element(ranks.begin(),ranks.end());
        ll r=1ll*maxWord*cars*cars;
        ll ans=-1;
        while(l<=r){
            ll mid=l+(r-l)/2;
            if(isPossible(ranks,cars,mid)){
                ans=mid;
                r=mid-1;
            } else{
                l=mid+1;
            }
        }
        return ans;
    }
};
```
# Have a Good Day 😊 UpVote?
['Array', 'Binary Search', 'C++']