## 🔥 C++ | 🚀 Fast & Optimized | 🔍 binary-search - 🍭 greedy - 📊 array - 🎯 two-pointer  
IntuitionThe goal is to distribute candies fairly among k children.We need to find the maximum possible number of candies per childsuch that at least k children
![Screenshot 2025-03-14 083542.png](https://assets.leetcode.com/users/images/b832d1bc-4051-49f8-a0dd-c73e168dc7a7_1741934157.1581588.png)



## Intuition  
## The goal is to distribute candies fairly among `k` children.  
## We need to find the **maximum possible number of candies per child**  
## such that at least `k` children get that amount.  

## We can use **Binary Search** 🧐 to efficiently find this maximum value.  

## Approach  
## 1️⃣ **Define the search space:**  
##    - Minimum possible candies per child = `1` 🍬  
##    - Maximum possible = `sum(candies)`, if one child takes everything.  

## 2️⃣ **Use Binary Search to find the best value:**  
##    - Pick a mid-value `mid = (left + right) / 2`.  
##    - Check how many children can get `mid` candies.  
##    - If at least `k` children can get `mid`, try a larger value.  
##    - Otherwise, try a smaller value.  

## 3️⃣ **Continue until the best valid value is found.**  

## Complexity  
## - **Time Complexity:** `O(N log M)` 🕒  
##   - `N` = number of candy piles.  
##   - `M` = total number of candies.  
##   - We do a binary search (`log M` steps), and each step checks all candies (`O(N)`).  

## - **Space Complexity:** `O(1)` 💾  
##   - We use only a few extra variables, no extra space needed.  


# Code
```cpp []
// بسم الله الرحمن الرحيم  
// والصلاة والسلام على رسول الله ﷺ  
// Author: Ahmed Elshamy  
// In the name of Allah, the Most Gracious, the Most Merciful.  
// May peace and blessings be upon the Prophet Muhammad ﷺ.  
// Welcome to my solution! I hope you find it useful 🚀  
// مرحبًا بكم في كودي! أتمنى لكم تجربة ممتعة في البرمجة التنافسية 💡🔥


#include <bits/stdc++.h>
using namespace std;

#define ll long long

class Solution
{
public:
    int maximumCandies(vector<int> &candies, ll k)
    {

        ll total_candies = accumulate(candies.begin(), candies.end(), 0LL);

        // If total candies are less than k, it's impossible to distribute
        if (total_candies < k)
            return 0;

        ll left = 1, right = total_candies, max_size = 0;

        while (left <= right)
        {
            ll mid = left + (right - left) / 2; // Prevent overflow

            ll child_count = 0; // Tracks how many children can receive candies

            // Count how many children can get at least `mid` candies
            for (const auto &candy : candies)
            {
                child_count += candy / mid;
                if (child_count >= k)
                    break; // No need to check further
            }

            if (child_count >= k)
            { // Valid distribution, try larger size
                max_size = mid;
                left = mid + 1;
            }
            else
            { // Not enough children can get `mid` candies
                right = mid - 1;
            }
        }
        return max_size;
    }
};
```
['Array', 'Binary Search', 'C++']