Binary Search | Minimum Time to Repair Cars
IntuitionThe problem involves finding the minimum possible time required to repair a given number of cars using mechanics of different efficiency levels. Since
# Intuition
The problem involves finding the **minimum possible time** required to repair a given number of cars using mechanics of different efficiency levels. Since **time is a monotonic function** (if a time `t` is possible, then any `t' > t` is also possible), **Binary Search on Answer** is a suitable approach.

# Approach
1. **Define Search Space:**  
   - The minimum possible time (`left`) is `1`.
   - The maximum possible time (`right`) is determined by the worst mechanic (i.e., `ranks[0] * cars * cars`). However, this should be adjusted using the any of the given or minimum rank in the array.

2. **Binary Search on Time:**  
   - Find the middle time (`mid`).
   - Check if it is **possible** to repair `cars` within `mid` time.
   - If **possible**, update `result` and search for a **smaller** time (`right = mid - 1`).
   - Otherwise, search for a **larger** time (`left = mid + 1`).

3. **Feasibility Check (`isPossible` method):**  
   - For each mechanic, compute the number of cars they can repair within `mid` time using the formula:  
     \[
     \text{carsMade} += \lfloor \sqrt{\frac{\text{mid}}{\text{rank}_i}} \rfloor
     \]
   - If the total number of repaired cars reaches or exceeds `cars`, return `true`.

# Complexity
- **Time complexity:**  
  - **Binary Search:** \( O(\log(\text{minRank} \times \text{cars}^2)) \)  
  - **Feasibility Check:** \( O(n) \)  
  - **Total:** \( O(n \log(\text{minRank} \times \text{cars}^2)) \)

- **Space complexity:**  
  - \( O(1) \) (Only a few extra variables used)

# Code
```java
class Solution {
    public long repairCars(int[] ranks, int cars) {

        long result = -1;
        long left = 1, right = (long) ranks[0] * cars * cars;

        while (left <= right) {
            long mid = left + (right - left) / 2;

            if (isPossible(mid, ranks, cars)) {
                result = mid;

                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }

        return result;

    }

    public boolean isPossible(long mid, int[] ranks, int cars) {
        long carMade = 0;

        for(int rank : ranks){
            carMade += (long)Math.sqrt(1.0 * mid/rank);
            if (carMade >= cars) return true;
        }

        return false;
    }
}

['Array', 'Binary Search', 'Java']