You are given a 0-indexed integer array candies. Each element in the array denotes a pile of candies of size candies[i]. You can divide each pile into any number of sub piles, but you cannot merge two piles together.

You are also given an integer k. You should allocate piles of candies to k children such that each child gets the same number of candies. Each child can take at most one pile of candies and some piles of candies may go unused.

Return the maximum number of candies each child can get.
&nbsp;
Example 1:

Input: candies = [5,8,6], k = 3
Output: 5
Explanation: We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.


Example 2:

Input: candies = [2,5], k = 11
Output: 0
Explanation: There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.


&nbsp;
Constraints:


	1 &lt;= candies.length &lt;= 105
	1 &lt;= candies[i] &lt;= 107
	1 &lt;= k &lt;= 1012


def canSplit(candies, mid, k):
    split = 0
    for i in candies:
        split += i//mid
    if split >= k:
        return True
    else:
        return False

class Solution:
    def maximumCandies(self, candies: List[int], k: int) -> int:
        end = sum(candies)//k
        start = 1
        ans = 0
        while start <= end:
            mid = (start + end)//2
            if canSplit(candies, mid, k):
                start = mid + 1
                ans = mid
            else:
                end = mid - 1
        return ans
class Solution {
    public boolean canSplit(int[] candies, long k, long mid) {
        long split = 0;
        for(int i = 0; i < candies.length; ++i) {
            split += candies[i]/mid;
        }   
        if(split >= k)
            return true;
        else
            return false;
    }
    
    public int maximumCandies(int[] candies, long k) {
        long sum = 0;
        for(int i = 0; i < candies.length; ++i) {
            sum += candies[i];
        }
        long start = 1, end = sum;
        long ans = 0;
        while(start <= end) {
            long mid = (start + end)/2;
            if(canSplit(candies, k, mid)) {
                ans = mid;
                start = mid + 1;
            } else {
                end = mid-1;
            }
        }
        return (int)ans;
    }
}
class Solution {
public:
    bool canSplit(vector<int>& candies, long long k, long long mid) {
        long long split = 0;
        for(int i = 0; i < candies.size(); ++i) {
            split += candies[i]/mid;
        }   
        if(split >= k)
            return true;
        else
            return false;
    }
    
    int maximumCandies(vector<int>& candies, long long k) {
        long long sum = 0;
        for(int i = 0; i < candies.size(); ++i) {
            sum += candies[i];
        }
        long long start = 1, end = sum/k;
        long long ans = 0;
        while(start <= end) {
            long long mid = (start + end)/2;
            if(canSplit(candies, k, mid)) {
                ans = mid;
                start = mid + 1;
            } else {
                end = mid-1;
            }
        }
        return ans;
    }
};
var maximumCandies = function(candies, k) {
    const n = candies.length;
    
    let left = 1;
    let right = 1e7 + 1;
    
    while (left < right) {
        const mid = (left + right) >> 1;
        const pilesAvail = divideIntoPiles(mid);

        if (pilesAvail < k) right = mid;
        else left = mid + 1;
    }
    
    return right - 1;
    
    
    function divideIntoPiles(pileSize) {
        let piles = 0;
        
        for (let i = 0; i < n; ++i) {
            const count = candies[i];
            
            piles += Math.floor(count / pileSize);
        }
        
        return piles;
    }
};
Maximum Candies Allocated to K Children