[Binary Search🔥] | Beginner Friendly Explanation🚀 | Java, Python, C++, JavaScript
IntuitionWe need to find the minimum time required to repair a given number of cars using mechanics with different skill levels.
Each mechanic has a rank, and t
# Intuition  
We need to find the **minimum time** required to repair a given number of cars using mechanics with different skill levels.  
- Each mechanic has a `rank`, and the time taken to repair `x` cars follows the formula:  
  $$ \text{time} = \text{rank} \times (\text{cars}^2) $$
- The goal is to **minimize the maximum time** among all mechanics.

A **binary search** approach works well because:
- The more time we allow, the more cars can be repaired.
- If a given time `mid` is feasible, a smaller time might also be feasible.

# Approach  
1. **Sort the ranks** to prioritize efficient mechanics.  
2. **Binary search on time**:  
   - Set `l = 1` (minimum time) and `r = max_rank * cars²` (worst case).  
   - Calculate `mid = (l + r) / 2` and check if it's possible to repair `cars` within `mid` time.  
   - If possible, update `time` and search in the left half (`r = mid - 1`), else search in the right half (`l = mid + 1`).  
3. **Checking feasibility (`isPossible` function)**:  
   - For each mechanic, calculate the maximum number of cars they can repair in `mid` time:  
     $$ \text{cars} = \lfloor \sqrt{\text{time} / \text{rank}} \rfloor $$
   - Sum up the cars repaired and return `true` if it meets or exceeds `cars`.  
4. **Return the minimum feasible time**.  

# Complexity  
- **Time Complexity:** $$O(n \log (\text{maxRank} \times \text{cars}^2))$$  
- **Space Complexity:** $$O(1)$$  

# Code  

```java []
import java.util.*;

class Solution {
    public long repairCars(int[] ranks, int cars) {
        long time = Long.MAX_VALUE;
        Arrays.sort(ranks); // Sort ranks for efficiency
        long l = 1, r = (long) ranks[ranks.length - 1] * cars * cars; // Max possible time
        
        while (l <= r) {
            long mid = l + (r - l) / 2;
            
            if (isPossible(ranks, cars, mid)) {
                time = Math.min(time, mid);
                r = mid - 1; // Try to minimize time
            } else {
                l = mid + 1;
            }
        }
        return time;
    }

    // Checks if it's possible to repair 'cars' in 'time'
    public boolean isPossible(int[] ranks, int cars, long time) {
        int repairedCars = 0;
        for (int rank : ranks) {
            repairedCars += Math.sqrt(time / rank); // Max cars this mechanic can repair
            if (repairedCars >= cars) return true; // No need to check further
        }
        return false;
    }
}
```
```python3 []
import math

class Solution:
    def repairCars(self, ranks: list[int], cars: int) -> int:
        ranks.sort()
        l, r = 1, ranks[-1] * cars * cars
        time = r

        while l <= r:
            mid = (l + r) // 2
            if self.isPossible(ranks, cars, mid):
                time = mid
                r = mid - 1  # Search for smaller time
            else:
                l = mid + 1

        return time

    def isPossible(self, ranks: list[int], cars: int, time: int) -> bool:
        repairedCars = sum(int(math.sqrt(time // rank)) for rank in ranks)
        return repairedCars >= cars
```
```cpp []
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

class Solution {
public:
    long long repairCars(vector<int>& ranks, int cars) {
        sort(ranks.begin(), ranks.end()); // Sort to prioritize efficient mechanics
        long long l = 1, r = (long long)ranks.back() * cars * cars, time = r;

        while (l <= r) {
            long long mid = l + (r - l) / 2;
            if (isPossible(ranks, cars, mid)) {
                time = mid;
                r = mid - 1; // Try to minimize the time
            } else {
                l = mid + 1;
            }
        }
        return time;
    }

private:
    bool isPossible(vector<int>& ranks, int cars, long long time) {
        int repairedCars = 0;
        for (int rank : ranks) {
            repairedCars += sqrt(time / rank); // Max cars this mechanic can repair
            if (repairedCars >= cars) return true;
        }
        return false;
    }
};
```
```javascript []
var repairCars = function(ranks, cars) {
    ranks.sort((a, b) => a - b); // Sort ranks for efficiency
    let l = 1, r = ranks[ranks.length - 1] * cars * cars, time = r;

    while (l <= r) {
        let mid = Math.floor((l + r) / 2);
        if (isPossible(ranks, cars, mid)) {
            time = mid;
            r = mid - 1; // Try to minimize time
        } else {
            l = mid + 1;
        }
    }
    return time;
};

// Checks if it's possible to repair 'cars' in 'time'
function isPossible(ranks, cars, time) {
    let repairedCars = 0;
    for (let rank of ranks) {
        repairedCars += Math.floor(Math.sqrt(time / rank)); // Max cars this mechanic can repair
        if (repairedCars >= cars) return true;
    }
    return false;
}
```

**Please Upvote⬆️** 🚀
![Screenshot 2025-02-26 230220.png](https://assets.leetcode.com/users/images/ee6a8aa7-9e14-4876-99ce-1bbd0626e31c_1742106473.2518349.png)

['Array', 'Binary Search', 'C++', 'Java', 'Python3', 'JavaScript']