Binary Search Approach for Maximum Candies Allocation
IntuitionWhen distributing candies among k children, each child must receive the same amount, and the candies from a single pile cannot be merged with others. T
## Intuition
When distributing candies among `k` children, each child must receive the same amount, and the candies from a single pile cannot be merged with others. The key observation is that the maximum number of candies each child can receive must be some integer `x` such that there are at least `k` piles of size `x`.

Since we can split the piles but not merge them, our goal is to determine the largest possible value of `x` for which there exist at least `k` piles.

## Approach
We use **binary search** on the possible values of `x`. The lowest possible value is `1` (each child gets at least one candy), and the highest possible value is `max(candies)`, since no child can receive more candies than what exists in the largest pile.

1. **Define a helper function:** This function checks whether it is possible to distribute at least `k` piles of size `x` by counting how many full piles of size `x` can be made from the given candy piles.
2. **Binary Search:** We perform binary search between `1` and `max(candies)`. For each middle value (`midValue`):
   - If it's possible to form `k` piles of at least `midValue` candies, we try increasing `midValue` (search in the upper half).
   - Otherwise, we search in the lower half.
3. **Return the largest valid `midValue` found.**

## Complexity
- **Time Complexity:** $$O(n \log m)$$, where:
  - $$n$$ is the number of elements in `candies`.
  - $$m$$ is the maximum value in `candies`.
  - The binary search runs in $$O(\log m)$$, and for each search step, we iterate through `candies` in $$O(n)$$.

- **Space Complexity:** $$O(1)$$, since we use only a few extra variables for computation.

## Code
```typescript
function maximumCandies(candies: number[], k: number): number {
    let minValue = 1;
    let maxValue = Math.max(...candies);
    
    const helper = (num: number): boolean => {
        let sum = 0;
        for (const candie of candies) {
            sum += Math.floor(candie / num);
        }
        return sum >= k;
    };
    
    while (minValue <= maxValue) {
        const midValue = Math.floor((minValue + maxValue) / 2);
        if (helper(midValue)) {
            minValue = midValue + 1;
        } else {
            maxValue = midValue - 1;
        }
    }
    return minValue - 1;
}
```


['TypeScript']