Binary Search
Say we decide to allocate m candies. We can check if all k kids can get m candies in O(n).\n\nTherefore, we can binary-search for the maximum value m. The overa
Say we decide to allocate `m` candies. We can check if all `k` kids can get `m` candies in O(n).\n\nTherefore, we can binary-search for the maximum value `m`. The overall runtime complexity will be O(n * log m), where `m` is maximum number of candies in a single pile (10000000).\n\nThe fact that we can split piles could be confusing. But it simply means that we can distribute `m` candies to ` candies[i] / m` children from pile `i`.\n\n**C++**\n```cpp    \nint maximumCandies(vector<int>& candies, long long k) {\n    int l = 0, r = 10000000;\n    while(l < r) {\n        long long m = (l + r + 1) / 2, cnt = 0;\n        for (int i = 0; i < candies.size() && cnt < k; ++i)\n            cnt += candies[i] / m;\n        if (cnt >= k)\n            l = m;\n        else\n            r = m - 1;\n    }\n    return l;\n}\n```
[]