Beats 99% || Simple Binary Search || Easy Understand || Beginner friendly Solution
IntuitionWe need to determine the minimum time required to repair all cars, given that mechanics with different ranks take different amounts of time to repair a
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

We need to determine the minimum time required to repair all cars, given that mechanics with different ranks take different amounts of time to repair a car. The time a mechanic takes to repair \( k \) cars follows the formula:

time = r * n^2

where \( r \) is the mechanic's rank. Since this is a **monotonic function**, binary search on the time range is a suitable approach.

---


# Approach
<!-- Describe your approach to solving the problem. -->
1. **Define the Search Space:**
   - The minimum possible time is \( 0 \).
   - The maximum possible time occurs when the slowest mechanic (highest rank) repairs all cars alone, given by:
      `right=*max_element(ranks.begin(), ranks.end())*cars*cars;`
   
2. **Binary Search on Time:**
   - Set `left = 0` and `right = max_rank * cars^2`.
   - Check if a given `mid` time is sufficient to repair all cars.
   - If possible, update `ans` and move `right` to `mid - 1` to find a smaller valid time.
   - Otherwise, move `left` to `mid + 1`.

3. **Feasibility Check (`CheckAllCarRepairPossible` function):**
# Solving for
   - Each mechanic with rank \( r \) can repair: **rank×n^2 ≤ Time**
**𝑛^2 ≤ Time/𝑟ank**
**𝑛 ≤ square root(Time/𝑟ank)**

- cars within the given `time`.
- Sum up the number of cars repaired by all mechanics.
- If this sum is at least the required `cars`, return `true`.

---
# Complexity
- Time complexity: O(N∗Log(Max Element))
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:\( O(1) \), since only a few extra variables are used.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int n;
    
    // Function to check if all cars can be repaired within a given time
    bool CheckAllCarRepairPossible(vector<int>& ranks, long long time, int cars) {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += sqrt(time / ranks[i]); // Cars repaired by current mechanic
            if (sum >= cars) return true; // Early stopping condition
        }
        return false;
    }

    long long repairCars(vector<int>& ranks, int cars) {
        long long left = 0;
        long long right = *max_element(ranks.begin(), ranks.end()) * (long long)cars * cars;
        n = ranks.size();
        long long ans = right;

        // Binary search on time
        while (left <= right) {
            long long mid = left + (right - left) / 2;
            if (CheckAllCarRepairPossible(ranks, mid, cars)) {
                ans = mid;  // Possible answer found
                right = mid - 1;  // Try to minimize time
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }
};
```
---

# ⬆️Upvote if it helps ⬆️

---
# Thank You Watching my code
['Array', 'Binary Search', 'C++']