Maximum Candies Distribution - Binary Search (Easiest and Optimal approach)
IntuitionThe problem boils down to distributing candies into k piles as evenly as possible, ensuring each child gets the maximum number of candies. The intuitio
# Intuition
The problem boils down to distributing candies into k piles as evenly as possible, ensuring each child gets the maximum number of candies. The intuition here is to treat the problem like a "binary search on answers" — where the answer is the maximum number of candies a child can get. We guess a possible solution (mid candies per child) and check if it's feasible.

If we can distribute candies to satisfy all k children with this amount (mid), we try for a higher number (mid + 1), aiming for a better result. Otherwise, we reduce our guess (mid - 1).

This search strategy ensures we efficiently hone in on the maximum possible candies per child.

# Approach
Define the Search Space:
The minimum number of candies per child is 1 (smallest possible split), and the maximum is the largest pile (max(candies)).

**Binary Search Setup:**

Set start = 1 and end = max(candies).
For each mid = (start + end) / 2, check if it's feasible to give each child mid candies.
Feasibility Check (can_assign function):

Count how many children can get mid candies using each pile (count += candies[i] / mid).
If count >= k, the current mid is feasible, and we try for a larger value (start = mid + 1).
Otherwise, reduce the search space (end = mid - 1).
Result:

Track the largest feasible mid in res and return it at the end.

# Complexity

- Time complexity:
Each binary search iteration halves the search space, giving ***O(log(max(candies)))*** iterations. 
For each guess, we loop through the candies array to count feasible splits, which takes ***O(n)*** times, hence total time is ***O(n * log(max(candies)))***.

- Space complexity:
We use extra variables (count, start, end, mid) so it results in **O(1)** extra space. The candies array is given as input, so it doesn't count towards extra space.

Hope you'll find this solution useful!

# Code
```cpp []
class Solution {
public:
    #define ll long long
    bool can_assign(long long& mid, vector<int>& candies, long long& k){
        ll count = 0;
        for (ll i : candies){
            if (i >= mid){
                count += i/mid;
            }
        }
        return count >= k;
    }
    int maximumCandies(vector<int>& candies, long long k) {
        ll start = 1;
        ll end = *max_element(candies.begin(), candies.end());
        ll mid;

        ll res = 0;
        while(start <= end){
            mid = start + (end - start)/2;
            if(can_assign(mid,candies,k)){
                res = mid;
                start = mid + 1;
            }
            else{
                end = mid - 1 ;
            }
        }
        return res;
    }
};
```
['Array', 'Binary Search', 'C++']