4 Lines | Concise | Binary Search | C++ | Python | Java | Go | JS
Implementation
# Implementation
```cpp []
class Solution {
public:
    int maximumCandies(vector<int>& A, long long k , int l = 0 , int r = 1e7) {
        while(l < r){
            int m = (l + r + 1)/2; long s = 0;
            for(auto p : A) s += p/m;
            if(s < k) r = m - 1; else l = m;
        }
        return l;
    }
};
```
```Java []
class Solution {
    public int maximumCandies(int[] candies, long k) {
        int l = 0, r = (int)1e7;
        while (l < r) {
            int m = (l + r + 1) / 2;
            long s = 0;
            for (int p : candies) s += p / m;
            if (s < k) r = m - 1;
            else l = m;
        }
        return l;
    }
}
```
```Python []
class Solution:
    def maximumCandies(self, A, k):
        l, r = 0, int(1e7)
        while l < r:
            m = (l + r + 1) // 2
            s = sum(p // m for p in A)
            if s < k:
                r = m - 1
            else:
                l = m
        return l
```
```Go []
func maximumCandies(A []int, k int64) int {
    l, r := 0, int(1e7)
    for l < r {
        m := (l + r + 1) / 2
        var s int64
        for _, p := range A {
            s += int64(p / m)
        }
        if s < k {
            r = m - 1
        } else {
            l = m
        }
    }
    return l
}
```
```Javascript []
var maximumCandies = function(A, k) {
    let l = 0, r = 1e7;
    while (l < r) {
        let m = Math.floor((l + r + 1) / 2);
        let s = A.reduce((sum, p) => sum + Math.floor(p / m), 0);
        if (s < k) r = m - 1;
        else l = m;
    }
    return l;
};
```
['Array', 'Binary Search', 'Python', 'C++', 'Java', 'Go', 'JavaScript']