✅Binary Search 🔥| Visualization ✨| Math | Python | C++ | Java ✨
Approach
Complexity

Time complexity:   O(nlogX)


Space complexity:  O(1)

Code
# Approach
![WhatsApp Image 2025-03-16 at 09.29.44_8715f0bd.jpg](https://assets.leetcode.com/users/images/860dac9f-85c0-4f64-aeca-befc7241216d_1742097986.7329016.jpeg)
![WhatsApp Image 2025-03-16 at 09.36.18_250a951b.jpg](https://assets.leetcode.com/users/images/8c6bb7a6-1956-40c1-80c6-44f177d921d3_1742097996.8588595.jpeg)



# Complexity
- Time complexity:   $$O(nlogX)$$

- Space complexity:  $$O(1)$$

# Code
```python []
import math

class Solution(object):
    def time_is_suff(self, ranks, cars, min_given):
        cars_done = 0
        for r in ranks:
            c2 = min_given // r
            c = int(math.sqrt(c2)) 
            cars_done += c
        return cars_done >= cars

    def repairCars(self, ranks, cars):
        l, r = 1, int(1e14)
        while l < r:
            mid = (l + r) // 2
            if self.time_is_suff(ranks, cars, mid):
                r = mid
            else:
                l = mid + 1
        return l

```
```C++ []
#include <cmath>
#include <vector>
using namespace std;

class Solution {
public:
    bool timeIsSuff(vector<int>& ranks, int cars, long long minGiven) {
        long long carsDone = 0;
        for (int r : ranks) {
            long long c2 = minGiven / r;
            long long c = floor(sqrt(c2));
            carsDone += c;
        }
        return carsDone >= cars;
    }
    
    long long repairCars(vector<int>& ranks, int cars) {
        long long l = 1, r = 1e14;
        while (l < r) {
            long long mid = (l + r) / 2;
            if (timeIsSuff(ranks, cars, mid)) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }
};

```
```Java []
class Solution {
    public boolean timeIsSuff(int[] ranks, int cars, long minGiven) {
        long carsDone = 0;
        for (int r : ranks) {
            long c2 = minGiven / r;
            long c = (long) Math.sqrt(c2);
            carsDone += c;
        }
        return carsDone >= cars;
    }

    public long repairCars(int[] ranks, int cars) {
        long l = 1, r = (long) 1e14;
        while (l < r) {
            long mid = (l + r) / 2;
            if (timeIsSuff(ranks, cars, mid)) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }
}
```
![image.png](https://assets.leetcode.com/users/images/4a8d9cea-9ec3-4e39-b999-57571ac4d18e_1742098193.4775264.png)

['Array', 'Math', 'Binary Search', 'Python', 'C++', 'Java']