binary search of Koko's type||3ms Beats 99.82%
IntuitionUse binary search.
C++ & Python are doneApproachbool get_c(vector<int>& candies, long long k, int c) is defined to judge whether every child can get c
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Use binary search.
C++ & Python are done
# Approach
<!-- Describe your approach to solving the problem. -->
`bool get_c(vector<int>& candies, long long k, int c)` is defined to judge whether every child can get c candies for k children.

It's to note during the loop `while(l<r){...}`, when `l=m, r=m-1` is used **the upper mid should be taken**, whereas the lower mid is used if `l=m+1, r=m` is used.

Otherwise it is very similar to solve [875. Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/solutions/3685871/w-explanation-easy-binary-search-c-solution/)
[Please turn on the English subtitles]
[https://youtu.be/KtZOZ4E1zww?si=o3rhF07qrvaTJn1z](https://youtu.be/KtZOZ4E1zww?si=o3rhF07qrvaTJn1z)
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$O(\log (sum/k+n) n)$ where n=|candies|, sum=sum(candies)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$O(1)$
# Code||C+++ 3ms Beats 99.82%
```cpp []
class Solution {
public:
    bool get_c(vector<int>& candies, long long k, int c) {
        [[unroll]]
        for (int x : candies) {
            k-=x/c;
            if (k<=0) return 1;
        }
        return 0;
    }

    int maximumCandies(vector<int>& candies, long long k) {
        const long long sum=accumulate(candies.begin(), candies.end(), 0LL);
        if (sum<k) return 0;

        int l=1, r=sum/k, m;  // sum/k as the upper bound
        while (l<r) {
            m=l+(r-l+1)/2;  // upper mid to avoid infinite loop
            if (get_c(candies, k, m)) l=m;
            else r=m-1;
        }
        return l;
    }
};

```
```Python []
class Solution:
    def maximumCandies(self, candies: List[int], k: int) -> int:
        def get_c(c, k):
            for x in candies:
                k-=x//c
                if k<=0: 
                    return True
            return False
        Sum=sum(candies)
        if Sum<k: 
            return 0
        l, r=1, Sum//k
        while l<r:
            m=(l+r+1)//2
            if get_c(m, k): 
                l=m
            else: 
                r=m-1
        return l
        
        
```
['Binary Search', 'C++', 'Python3']