Easy Solution 👍 | Beginner Friendly 😊| Binary Search 🔥| 100% beats ✅ | 0(1) SC🔥
IntuitionThis question is almost similar to the famous "Aggressive Cow" problem where we pick up random numbers from the range and check how many cows can be di
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
This question is almost similar to the famous "Aggressive Cow" problem where we pick up random numbers from the range and check how many cows can be distributed.

# Approach
<!-- Describe your approach to solving the problem. -->
see the maximum possible number of candies we can distribute is the maximum size of piles as we can't merge any two piles. And minimum possibility will be 1. Now we will be using binary search algo to pick up a random number from the range defined and will check whether it can be valid answer or not. 

So in the checking function we will be checking the possiblity. A number of candies n(say) can be a valid answer if sum of all the quotient of (Candies[i]/n) is greater than or equal to total numbers of children. In this case we'll be returning true otherwise false.

If it's true then we'll be upgrading the low to mid+1 and otherwise changing high to mid-1.

As we need the max possible answer so we'll be returning high value.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ --> O(N∗Log(Max(C))

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ --> 0(1)

# Code
```cpp []
class Solution
{

public:
    bool Check(vector<int> c, int candy, long long k)
    {
        long long total = 0;
        for (int i : c)
        {
            total += (i / candy);
        }
        if (total >= k)
            return true;
        return false;
    }

    int maximumCandies(vector<int> &candies, long long k)
    {
        int n = candies.size();
        int high = *max_element(candies.begin(), candies.end());
        int low = 1;
        if (k == 0)
            return 0;
        while (low <= high)
        {
            int mid = low + (high - low) / 2;
            if (Check(candies, mid, k))
                low = mid + 1;
            else
                high = mid - 1;
        }
        return high;
    }
};


```
['C++']