Have some candies :) 🍭🍭
THINKcandies = [5, 8, 6], k = 3TC:
Binary search range: O(log(max(candies)))
Each iteration: O(n) to compute count
Total complexity: O(n log m), where m = max(c
# THINK
candies = [5, 8, 6], k = 3

![image.png](https://assets.leetcode.com/users/images/2d7b6806-b32e-4274-9550-addb4233d5a9_1741932676.6579456.png)



# TC:
- Binary search range: O(log(max(candies)))
- Each iteration: O(n) to compute count
- Total complexity: O(n log m), where m = max(candies)
- This is an efficient solution compared to brute force.


# Code
```cpp []
class Solution {
public:
// Why kids only have all the fun 
    // Your candies 🍭🍭
    int maximumCandies(vector<int>& candies, long long k) {
        int left = 1; // This is the min candies in the pile
        int right = *max_element(candies.begin(),candies.end());
                       // max_element returns the max element in the arr
        int result = 0;
       //Using the binary search 
        while(left<=right){
            long long mid = left + (right - left)/2;
            long long count = 0;
        
        for(int c : candies){
            count += c/mid;
        }
        if(count>=k){
            result = mid;
            left = mid+1;
        }
        else{
            right= mid-1;
        }

        }
        return result;  
    }
};
```
['Array', 'Binary Search', 'C++']