✅ Easy to Understand | Binary Search on Answers | Detailed Video Explanation🔥
IntuitionThe problem requires us to find the minimum time needed to repair all cars, given that multiple mechanics with different efficiency levels can work sim
# Intuition
The problem requires us to find the minimum time needed to repair all cars, given that multiple mechanics with different efficiency levels can work simultaneously. The key observation is that the time taken by a mechanic to repair `n` cars follows a quadratic relationship: `time = rank * n^2`.  

Since we need to minimize the maximum time taken, we can use **binary search on time** to efficiently find the smallest feasible time in which all cars can be repaired.

# Approach
1. **Binary Search on Time:**  
   - Define the search space for binary search:
     - The minimum possible time (`l`) is `1` (best case scenario).  
     - The maximum possible time (`r`) is `max(rank) * cars^2` (worst case, where the slowest mechanic repairs all cars).
   - Perform binary search to determine the minimum time `T` where all cars can be repaired.

2. **Helper Function to Check Feasibility:**  
   - For a given time `mid`, calculate how many cars each mechanic can repair using:  
     \[
     \text{cars repaired} = \sqrt{\frac{\text{mid}}{\text{rank}}}
     \]
   - Sum up the cars repaired by all mechanics.  
   - If the total count is greater than or equal to the required cars, update the answer and search for a smaller time.

3. **Optimization Using Integer Square Root Calculation:**  
   - Instead of iterating through all time values, binary search reduces the number of checks to **logarithmic complexity**.  
   - Using `Math.sqrt(mid / rank)`, we efficiently estimate how many cars a mechanic can repair.

# Complexity
- **Time complexity:**  
  - The binary search runs in **O(log (max(rank) * cars²))** iterations.  
  - For each mid-time check, we iterate over `n` mechanics → **O(n)**.  
  - Hence, the total time complexity is **O(n log (max(rank) * cars²))**.

- **Space complexity:**  
  - We use only a few extra variables → **O(1) (constant space).**


# Code
```java []
class Solution {
    public long repairCars(int[] ranks, int cars) {
        // Define the search space for binary search
        long l = 1, r = ranks[0]; 
        
        // Find the maximum rank to determine the upper bound of search space
        for (int rank : ranks)
            r = Math.max(r, rank);
        
        // Maximum possible time if all cars are repaired by the slowest mechanic
        r = r * cars * cars;

        long ans = r; // Store the minimum possible time

        // Binary search to find the minimum time required
        while (l <= r) {
            long mid = l + (r - l) / 2; // Calculate the mid value

            // Check if it's possible to repair all cars within 'mid' minutes
            if (can(mid, ranks, cars)) {
                ans = mid; // Update the answer
                r = mid - 1; // Try for a smaller time
            } else {
                l = mid + 1; // Increase the time limit
            }
        }
        return ans; // Return the minimum time required
    }

    // Helper function to check if we can repair 'cars' within 'mid' minutes
    private boolean can(long mid, int[] ranks, int cars) {
        int count = 0; // Total cars that can be repaired within 'mid' time

        for (int rank : ranks) {
            count += Math.sqrt(mid / rank); // Calculate the number of cars a mechanic can repair
            if (count >= cars) return true; // If we can repair all cars, return true
        }

        return false; // Otherwise, return false
    }
}

```

https://youtu.be/Og1h_LWeJ_8
['Array', 'Binary Search', 'Java']