Easy C++ Solution | Step-wise Approach | Beats 93.28% | Binary Search
Intuition and Approach\n\n-- To find the minimum time required to repair all the cars, we can use binary search on the time range. We can start with a lower bou
# Intuition and Approach\n\n-- To find the minimum time required to repair all the cars, we can use binary search on the time range. We can start with a lower bound of 0 and an upper bound that\'s large enough to accommodate the sum of repair times for all cars. \n\n-- Then, we repeatedly narrow down the search space by checking if it\'s possible to repair all the cars within a certain time limit. If it\'s possible, we adjust the upper bound to search for smaller times; otherwise, we adjust the lower bound to search for larger times. We continue this process until we find the minimum time required to repair all the cars.\n\n-- Initialize a lower bound ($$low$$) to 0 and an upper bound ($$high$$) to a very large value, such as 1e14. Also, initialize a variable result to store the minimum time required to repair all cars.\n\n-- Perform binary search within the range [low, high]. In each iteration, calculate the midpoint ($$mid$$) of the range. Use the $$isValid$$ function to check if it\'s possible to repair all the cars within the time limit $$mid$$.\n\n-- This function should iterate through the repair ranks, calculating the cumulative repair time for each car under the given time limit. If the total repair time is sufficient to repair all the cars, return $$true$$; otherwise, return $$false$$.\n\n-- If it\'s possible, update $$result$$ to $$mid$$ and adjust high to mid - 1 to search for smaller times. If it\'s not possible, adjust $$low$$ to mid + 1 to search for larger times.\n\nContinue this process until low becomes greater than high. At this point, result will hold the minimum time required to repair all the cars. Return result. \n\n------------------\n#### If you find this solution helpful, consider giving it an upvote!\n\n##### Do check out all other solutions posted by me here: https://github.com/adityajamwal02/Leetcode-Community-Solutions\n\n---------------------\n\n# Code\n```\nclass Solution {\npublic:\n    bool isValid(vector<int> &ranks, int cars, long long mid){\n        long long counter=0;\n        for(auto it : ranks){\n            counter+=(sqrt(mid/it));\n        }\n    return counter>=cars;\n    }\n    long long repairCars(vector<int>& ranks, int cars) {\n        long long low=0, high=1e14, result=0;\n        while(low<=high){\n            long long mid=low+(high-low)/2;\n            if(isValid(ranks,cars,mid)){\n                result=mid;\n                high=mid-1;\n            }else{\n               low=mid+1;\n            }\n        }\n    return result;\n    }\n};\n```
['Array', 'Binary Search', 'C++']