C++ 🔍✨🚀 Binary Search ⚡🧐 | Math ➕➖✖️➗ | Greedy 🤑🔥 | Simulation 🎮🤖 
🧠 IntuitionHow do we solve this problem?🔹 We have multiple mechanics and a number of cars to repair.🔹 Each mechanic has a rank, which affects how long it takes
![Screenshot 2025-03-16 114423.png](https://assets.leetcode.com/users/images/7853d733-376c-4bf5-8bb2-6d55fb7c9bc0_1742118653.952242.png)



## **🧠 Intuition**  
## **How do we solve this problem?**  
## 🔹 We have **multiple mechanics** and a **number of cars** to repair.  
## 🔹 Each mechanic has a **rank**, which affects **how long it takes to repair cars**.  
## 🔹 **Goal:** Find the **minimum time** needed to repair **all cars**.  

## **💡 Key Idea:**  
## 🔹 Since **time is the key factor**, we can **binary search** on the minimum required time.  
## 🔹 For a given time `mid`, we check **if it's possible to repair all cars** within that time.  

---

## **🛠️ Approach**  
## **1️⃣ Use Binary Search on Time ⏳**  
## 🔹 We define **a range** for the time:  
## 🔹 The **minimum possible time** = `0`.  
## 🔹 The **maximum possible time** = `(max rank) * (cars²)`.  
## 🔹 We perform **binary search** to find the smallest valid time.  

## **2️⃣ Checking if `mid` time is enough ✅**  
## 🔹 For each mechanic with rank `r`, they can repair **`sqrt(mid / r)` cars**.  
## 🔹 If the **total repaired cars ≥ required cars**, then `mid` is a valid answer.  
## 🔹 Otherwise, we need **more time** and increase `mid`.  

## **3️⃣ Optimized Square Root Calculation 🔢**  
## 🔹 Instead of using `sqrt()`, we use **binary search to find the integer square root** to avoid precision issues.  
## 🔹 This prevents **integer overflow** when `mid` is very large.  

---

## **⏳ Complexity**  
## 🔹 **Binary Search on Time** → **`O(log (max_time))`**  
## 🔹 **Checking Feasibility for Each `mid`** → **`O(N log M)`**  
## 🔹 **Integer Square Root Calculation** → **`O(log M)`**  

## 🔹 **Total Complexity:** **`O(N log M log (max_time))`** 🚀  
## 🔹 **Space Complexity:** **`O(1)`** (Only using a few extra variables).  

---

## 🎯 **This approach ensures we efficiently find the minimum time required!** 🚗💨


# Code
```cpp []
// بسم الله الرحمن الرحيم  
// والصلاة والسلام على رسول الله ﷺ  
// Author: Ahmed Elshamy  
// In the name of Allah, the Most Gracious, the Most Merciful.  
// May peace and blessings be upon the Prophet Muhammad ﷺ.  
// Welcome to my solution! I hope you find it useful 🚀  
// مرحبًا بكم في كودي! أتمنى لكم تجربة ممتعة في البرمجة التنافسية 💡🔥

#include <bits/stdc++.h>
using namespace std;

#define ll long long

class Solution
{
public:
    ll repairCars(vector<int> &ranks, int cars)
    {
        unordered_map<int, int> freq;
        int minRank = INT_MAX;

        // Count the frequency of each rank and find the min rank
        for (int r : ranks)
        {
            ++freq[r];
            minRank = min(minRank, r);
        }

        ll left = 1, right = (ll)minRank * cars * cars, ans = -1;

        // Binary search on the minimum possible time
        while (left <= right)
        {
            ll mid = left + (right - left) / 2;
            ll remaining = cars;

            // Check how many cars can be repaired in 'mid' time
            for (const auto &[r, count] : freq)
            {
                ll maxCars = sqrt(mid / r); // Max cars each mechanic can repair
                remaining -= maxCars * count;
                if (remaining <= 0)
                    break; // Stop early if all cars are repaired
            }

            if (remaining <= 0)
            {
                ans = mid;
                right = mid - 1; // Try a smaller time
            }
            else
            {
                left = mid + 1; // Increase the time
            }
        }

        return ans;
    }
};

```
['Array', 'Hash Table', 'Math', 'Binary Search', 'C++']