Simple Binary Search & | Python | java | cpp | ✨✨
IntuitionApproachComplexity
Time complexity: O(N⋅log(M))
M = max(ranks) * cars * cars.

Space complexity: O(1)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: O(N⋅log(M))
      M = max(ranks) * cars * 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 {
    private long tot_cars(long min, int[] ranks) {
        long cnt = 0;
        for (int rank : ranks) {
            cnt += (long) Math.sqrt(min / rank);
        }
        return cnt;
    }

    public long repairCars(int[] ranks, int cars) {
        long low = 1;
        long high = (long) Arrays.stream(ranks).max().getAsInt() * cars * cars;

        while (low <= high) {
            long mid = (low + high) / 2;
            if (tot_cars(mid, ranks) >= cars) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return low;
    }
}

```
```python []
class Solution:
    def tot_cars(self, minimum, ranks):
        cnt = 0
        for rank in ranks:
            cnt += math.isqrt(minimum // rank)
        return cnt

    def repairCars(self, ranks: List[int], cars: int) -> int:
        low, high = 1, max(ranks) * cars * cars

        while low <= high:
            mid = (low + high) // 2
            if self.tot_cars(mid, ranks) >= cars:
                high = mid - 1
            else:
                low = mid + 1

        return low

```

```cpp []
typedef long long int ll;
class Solution {
    long long tot_cars(long long min, vector<int>& ranks){
        ll cnt = 0;
        for(int i = 0 ; i < ranks.size(); i++){
            cnt += sqrt(min/ranks[i]);
        }
        return cnt;
    }
public:
    long long repairCars(vector<int>& ranks, int cars) {
        ll low = 1;
        ll high = *max_element(ranks.begin(), ranks.end()) * 1LL * cars * cars;

        while (low <= high) {
            ll mid = (low + high) / 2;
            if (tot_cars(mid, ranks) >= cars)
                high = mid - 1;
            else
                low = mid + 1;
        }
        return low;
    }
};


```

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