Well explained solution-beats 91%
IntuitionWe need to distribute candies such that each child gets the same maximum number of candies. The challenge is that we can split a pile but cannot merge
# Intuition
We need to distribute candies such that each child gets the same maximum number of candies. The challenge is that we can split a pile but cannot merge piles together. This means the maximum number of candies a child can receive must be within the range [1, maxPileSize] (where maxPileSize is the largest pile).

To find the maximum valid number, we can use Binary Search because:

- The answer lies between 1 and maxPileSize.
- If a given number of candies per child X is possible, then all values less than X are also possible.
- If X is not possible, all values greater than X are also not possible.

Using binary search helps efficiently check for the largest possible value.

# Why Binary Search?
- We need to find the maximum number of candies per child (X).   Checking all possible values from 1 to maxPileSize would be inefficient (O(n * maxPileSize) in brute force). Binary Search efficiently narrows down the range in O(log maxPileSize) steps.   
- Since the answer space is monotonic (if X is possible, all values ≤ X are possible), binary search is the best approach.

# Approach
1. Find the largest pile (maxPileSize) in candies[] since the maximum possible candies per child cannot exceed this value.  
2. Use Binary Search within the range [1, maxPileSize]:
    - Mid = (l + r) / 2, representing the number of candies per child we are currently checking.
3. Use a helper function isPossibleToDistribute(candies, k, mid):
    - For each pile, calculate how many children can receive `mid` candies from that pile.
    - Sum up the total number of children that can be satisfied.
    - If at least `k` children are satisfied, `mid` is a valid candidate → move right (`l = mid + 1`) to try a higher number.
    - Otherwise, move left (`r = mid - 1`).
4. The final answer is the largest valid `mid` found.

# Complexity
- Time Complexity:
  - Finding the maxPileSize: $$O(n)$$
  - Binary Search range: $$O(\log \text{maxPileSize})$$
  - Checking each mid value (`isPossibleToDistribute` function): $$O(n)$$
  - Overall: $$O(n \log \text{maxPileSize})$$

- Space Complexity: $$O(1)$$
<!-- (Using only variables, no extra data structures) -->

# Code
```java []
class Solution {
    public int maximumCandies(int[] candies, long k) {
        int maxPileSize = 0;
        for(int candy:candies){
            maxPileSize = Math.max(candy, maxPileSize);
        }
        int l=1,r=maxPileSize,mid,ans=0;
        while(l<=r){
            mid = (l+r)/2;
            if(isPossibleToDistribute(candies, k, mid)){
                ans=mid;
                l=mid+1;
            }
            else{
                r=mid-1;
            }
        }
        return ans;
    }
    public boolean isPossibleToDistribute(int[] candies, long k, int distributeCandies){
        int numberOfChildren;
        for(int candiesInPile:candies){
            numberOfChildren = candiesInPile/distributeCandies;
            k -= numberOfChildren;
            if(k<=0){
                return true;
            }
        }
        return false;
    }
}
```
['Binary Search', 'Java']