Maximum Candies Allocated to K Children LeetCode solution in JavaScript
1var maximumCandies = function (candies, k) {2const n = candies.length;34let left = 1;5let right = 1e7 + 1;67while (left < right) {8const mid = (left + right) >> 1;9const pilesAvail = divideIntoPiles(mid);1011if (pilesAvail < k) right = mid;12else left = mid + 1;13}1415return right - 1;1617function divideIntoPiles(pileSize) {18let piles = 0;1920for (let i = 0; i < n; ++i) {21const count = candies[i];2223piles += Math.floor(count / pileSize);24}2526return piles;27}28};