Easy to understand || simple code || Binary Search || c++ || python || java 
Intuition
We need to maximize the number of candies that each person can receive. By checking if a candidate number of candies per person is feasible, we can us
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- We need to maximize the number of candies that each person can receive. By checking if a candidate number of candies per person is feasible, we can use binary search to efficiently find the maximum possible value.
# Approach
<!-- Describe your approach to solving the problem. -->
1. Create a helper function 'solve' that, given a candidate value `res`, calculates how many people can be served by summing up `candies[i] / res` for each candy pile.
2. In the main function, first check if the total number of candies is less than `k` (which means it's impossible to give even one candy per person).
3. Use binary search over the range [1, maximum candies in a single pile] to find the highest value of candies per person that can serve `k` people.
4. For each mid value in the binary search, use the helper function to determine if the candidate distribution is possible, then adjust the search range accordingly.

# Complexity
- Time complexity:O(n * log(max_candies))
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    bool solve(int res,vector<int>& candies, long long k){
        long long cnt=0;

        for(auto it:candies){
            cnt+=it/res;
        }

        return cnt>=k;
    }
    int maximumCandies(vector<int>& candies, long long k) {
        int n=candies.size();
        long long sum=0;
        for(auto it:candies){
            sum+=it;
        }
        if(sum<k){
            return 0;
        }

        int low=1;
        int high=*max_element(candies.begin(),candies.end());
        int ans=low;
        while(low<=high){
            int mid=(low+high)/2;
            if(solve(mid,candies,k)){
                ans=mid;
                low=mid+1;
            }else{
                high=mid-1;
            }
        }

        return ans;

    }
};
```

```python []

from typing import List

class Solution:
    def solve(self, res: int, candies: List[int], k: int) -> bool:
        cnt = 0
        for candy in candies:
            cnt += candy // res  
        return cnt >= k

    def maximumCandies(self, candies: List[int], k: int) -> int:
        
        if sum(candies) < k:
            return 0
        
        low = 1
        high = max(candies)
        ans = low
        
        
        while low <= high:
            mid = (low + high) // 2
            if self.solve(mid, candies, k):
                ans = mid  
                low = mid + 1  
            else:
                high = mid - 1 
        return ans


```
```java []
class Solution {
    private boolean solve(int res, int[] candies, long k) {
        long cnt = 0;
        for (int candy : candies) {
            cnt += candy / res;
        }
        return cnt >= k;
    }
    
    public int maximumCandies(int[] candies, long k) {
        long sum = 0;
        for (int candy : candies) {
            sum += candy;
        }
        if (sum < k) {
            return 0;
        }
        
        int low = 1;
        int high = 0;
        for (int candy : candies) {
            high = Math.max(high, candy);
        }
        int ans = low;
        
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (solve(mid, candies, k)) {
                ans = mid;
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return ans;
    }
}

```
['Array', 'Binary Search', 'C++', 'Java', 'Python3']