🍬 Maximum Candies Allocated to K Children 🍬 | 🎯 Easy Binary Search Explaination! 🚀 
IntuitionThe problem requires us to distribute candies among k children such that each child receives the maximum possible equal number of candies. The challeng
# Intuition
The problem requires us to distribute candies among `k` children such that each child receives the maximum possible **equal** number of candies. The challenge is to determine this maximum number efficiently.

To solve this, consider the following observations:
- If we know the number of candies each child should receive, we can easily check whether it's possible to distribute the candies among `k` children.
- The problem can be reframed as **a search for the maximum valid number of candies per child**.
- Since the number of candies per child ranges from `1` to `max(candies)`, we can use **binary search** to efficiently find the optimal value.

Using these insights, binary search becomes a natural choice for solving this problem in an optimized manner.

---

# Approach
### **Step 1: Define the Search Space**
We need to determine the maximum number of candies each child can get. The possible values range from `1` to `max(candies)`, so we define:
- `low = 1` (minimum possible candies per child)
- `high = max(candies)` (maximum possible candies per child from any pile)

### **Step 2: Implement Binary Search**
- **Compute the middle value `mid`**: This represents the candidate number of candies each child could receive.
- **Check if `mid` is feasible**:
  - Iterate over all candy piles and determine the number of children that can receive `mid` candies.
  - If the number of children `>= k`, increase `mid` (move right).
  - Otherwise, decrease `mid` (move left).

### **Step 3: Define the Check Function**
The function `check(candies, k, mid)` determines if it is possible to distribute `mid` candies per child to at least `k` children:
- Iterate through all candy piles.
- Compute how many full portions of `mid` can be made from each pile.
- Sum up these values to get the total number of children that can be served.
- If this total is `>= k`, return `true`; otherwise, return `false`.

### **Step 4: Binary Search Execution**
- Start with `low = 1`, `high = max(candies)`, and apply binary search.
- Continue adjusting the search range until `low > high`.
- The final answer is stored in `high`, representing the largest possible number of candies each child can receive.

---


# Complexity
[]()
- **Time complexity:**
    - **Binary search range:** The search range for `mid` is from `1` to `max(candies)`, which takes **`O(log max(candies))`** iterations.
    - **Checking feasibility (`check` function)**: Each check iteration requires a pass over `candies`, taking **`O(n)`**.
    - **Total complexity**: Since we perform **`O(log max(candies))`** binary search iterations, and each iteration runs in **`O(n)`**, the total complexity is:
    
[]()

- **Space complexity:**
    - We use a few extra variables (`low`, `high`, `mid`, `sum`) and do not use additional data structures.
    - The space complexity is **`O(1)`** (constant extra space).

---

# Code
```cpp []
class Solution {
private:
    long long check(vector<int>& candies, long long k, long long mid){
        long long cntStud=0;
        for(int i=0;i<candies.size();i++){
            cntStud+=candies[i]/mid;
        }
        return cntStud;
    }

public:
    int maximumCandies(vector<int>& candies, long long k) {
        long long low = 1;
        long long high=*max_element(candies.begin(),candies.end());
        long long sum = accumulate(candies.begin(), candies.end(), 0LL);
        if(sum<k) return 0;
        while (low <= high) {
            long long mid = low + (high - low) / 2;
            long long stud = check(candies, k, mid);
            if(stud>=k) low=mid+1;
            else high=mid-1;
        }
        return (int)high;
    }
};
```
['Binary Search', 'C++']