Simple Binary search | C++ | Well explained | Happy Holi !!!
IntuitionWe have to find that maximum number, candies should be equally distributed among k children.ApproachExample :
candies = {12, 34, 23, 56, 78, 45, 67, 89
# Intuition
We have to find that maximum number, candies should be equally distributed among k children.


# Approach
Example : 
candies = {12, 34, 23, 56, 78, 45, 67, 89, 90, 100}
k = 15

**Step 1**: Our final answer lies in 1 to maximum element(here 100).

**step 2**: check on binary search over 1 to 100 ( for this ex.)

See below table for this ex. how its work (image: ai generated but not solution)

![Screenshot 2025-03-14 062358.png](https://assets.leetcode.com/users/images/9312dca2-51bf-40b0-8499-e7dd3b6e5d03_1741913652.8824308.png)


# Complexity
- Time complexity: O(nlog(r)))
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

# Code
```cpp []
class Solution {
public:
bool check(vector<int>&candies, int &m, long long &k){
    long long ans = 0;
    for(int i=0;i<candies.size();++i){
        ans+=candies[i]/m;
    }
    return ans>=k;
}
    int maximumCandies(vector<int>& candies, long long k) {
      int l = 1, r = *max_element(candies.begin(),candies.end());

      while(l<=r){
        int m = l + (r-l)/2;
        if(check(candies,m,k)) l =m+1;
        else r = m-1;
      }
    return r;
    }
};
```

Yes, above code can be reduced into 4-5 lines but that is not our aim, understanding matters.
['Array', 'Binary Search', 'C++']