Optimal Time Calculation for Repairing Cars Using Binary Search || C++ || Easy Beginner's Friendly
IntuitionThe problem requires us to determine the minimum time required to repair a given number of cars using multiple workers, each with a different efficienc
# Intuition  
The problem requires us to determine the minimum time required to repair a given number of cars using multiple workers, each with a different efficiency. Each worker has a specific rank, which determines the time they take to repair a car.  

Observing the problem, we notice that:  
- A worker with rank \( r \) takes \( r \times k^2 \) time to repair \( k \) cars.  
- We need to distribute the work optimally among the workers to minimize the maximum time taken.  
- This suggests a **binary search** approach on the minimum possible time required to repair all cars.  

---

# Approach  
1. **Define the Search Space**:  
   - The minimum possible time is 1 (best case scenario).  
   - The maximum possible time is when the slowest worker (highest rank) repairs all the cars alone, which is \( r_{\text{min}} \times \text{cars}^2 \).  

2. **Binary Search on Time**:  
   - We use **binary search** to find the minimum time required.  
   - For a given time \( \text{mid} \), we check whether it is possible to repair all cars within this time.  
   - If yes, reduce the time limit (move left in the search space).  
   - If no, increase the time limit (move right in the search space).  

3. **Checking Feasibility (solve function)**:  
   - For each worker, calculate the maximum number of cars they can repair in the given time \( \text{mid} \) using the formula:  
     \[
     \text{cars per worker} = \sqrt{\frac{\text{mid}}{\text{rank}_i}}
     \]
   - If the total number of cars repaired by all workers is **greater than or equal to** the required cars, the time is sufficient.  

4. **Return the Minimum Time**:  
   - After binary search converges, the `start` value gives the minimum time required to repair all cars.  

---

# Complexity  
- **Time Complexity**:  
  - Sorting the ranks (if required) takes \( O(n \log n) \), but it is optional.  
  - The binary search runs in \( O(\log (\text{ranks}[0] \times \text{cars}^2)) \).  
  - Each feasibility check takes \( O(n) \).  
  - Overall, the complexity is **\( O(n \log (\text{ranks}[0] \times \text{cars}^2)) \)**.  

- **Space Complexity**:  
  - **\( O(1) \)**, as we are using only a few additional variables.


![upvote_please.png](https://assets.leetcode.com/users/images/13d4d3f2-411b-461b-bf50-5a3c603176d5_1742102011.4393606.png)


# Code
```cpp []
class Solution {

   bool solve(vector<int>& ranks,long long mid,int cars)
   {
    long long totalcars=0;
    for(int i=0;i<ranks.size();i++)
    {
        totalcars+=sqrt(mid/ranks[i]);
    }

    return totalcars>=cars;
   }
public:
    long long repairCars(vector<int>& ranks, int cars) {
        long long start = 1;
        // sort(ranks.begin(),ranks.end());
        long long end =(long long)ranks[0]*cars*cars;
        int n = ranks.size();
        long long mid = 0;
        
        while (start <=end) {
            mid = start + (end - start) / 2;
            if(solve(ranks,mid,cars))
            {
                end=mid-1;
            }
            else
            {
                start=mid+1;
            }
        }

       return start;
    }
};
```
['Array', 'Binary Search', 'C++']