Binary Search | Beats 78% | Java 
IntuitionThe problem requires finding the minimum time needed to repair cars cars using multiple mechanics, each having a different rank (efficiency). A mechani
![Screenshot 2025-03-16 095801.png](https://assets.leetcode.com/users/images/b898e9fe-7223-43d5-b205-c65c3ca2e742_1742099350.2861056.png)


# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires finding the minimum time needed to repair cars cars using multiple mechanics, each having a different rank (efficiency). A mechanic with rank r takes r * t^2 time to repair t cars. The goal is to minimize the maximum time required.

**Understanding the Problem**
1. Each mechanic can repair cars at different speeds based on their rank:
2. A mechanic with rank r takes r * t² time to repair t cars.
3. Given a fixed time mid, a mechanic with rank r can repair at most floor(√(mid / r)) cars.
4. Our task is to find the minimum mid such that all cars can be repaired.

# Approach
<!-- Describe your approach to solving the problem. -->
Since we are minimizing the time, we can apply binary search:

1. Define the Search Space:
    1. Upper bound (hi): max(ranks) * cars² (worst case: the slowest mechanic repairing all cars).
    This ensures we search for the optimal time within a feasible range.
2. Binary Search on Time (mid):

    1. Find mid: The candidate time we are checking.
    2. Check if it's possible (isitpossible function):
    3. Calculate the total number of cars that can be repaired in mid time.
    4. If total cars >= required cars (k), then mid is a valid solution. We try for a smaller mid (move hi left). Otherwise, increase mid (move lo right).

# Complexity
- Time complexity: O(n log(maxRank × cars²))
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public long repairCars(int[] ranks, int cars) {
        int n=ranks.length;
        long lo=1;
        long hi=(long)max(ranks)*cars*cars;
        long ans=0;
        while(lo<=hi){
            long mid=lo+(hi-lo)/2;
            if(isitpossible(ranks,cars,mid)){
                ans=mid;
                hi=mid-1;
            }
            else{
                lo=mid+1;
            }
        }
        return ans;        
    }
    public static boolean isitpossible(int [] arr,int k,long mid){
        long count=0;
        for(int i=0;i<arr.length;i++){
            count+=Math.sqrt(mid/arr[i]);
            if(count>=k){
                return true;
            }
        }
        return false;
    }
    public static int max(int [] arr){
        int max=arr[0];
        for(int i=1;i<arr.length;i++){
            max=Math.max(max,arr[i]);
        }
        return max;
    }
}
```
['Array', 'Binary Search', 'Greedy', 'Java']