The Simplest Approach Towards Better Solution
🍬 Problem Statement: Maximum Candies Allocation
You have an array of candies 🏗️, where each pile has some number of candies.
You need to distribute the candies

## **🍬 Problem Statement: Maximum Candies Allocation**
- You have an array of **candies** 🏗️, where each pile has some number of candies.
- You need to distribute the candies among **k children** 👶 such that:
  - Each child gets the **same** number of candies 🍫.
  - All candies taken by a child must come from a single pile.
- Find the **maximum** candies each child can get! 🎯

---

## **🔍 Approach: Binary Search on Answer**
### **Why Binary Search? 🤔**
Instead of checking all possible values manually **(brute force = slow 🚶‍♂️💨)**,  
using **Binary Search** helps find the answer efficiently **(fast 🚀)**.  

### **🔢 Steps**
1️⃣ **Find the search space**:  
   - `start = 1` (each child gets at least 1 candy) 🍬  
   - `end = max(candies[])` (no child can get more than the largest pile) 🎯  

2️⃣ **Apply Binary Search** 🧐  
   - Pick `mid = (start + end) / 2` (this is our current "guess" for max candies per child).  
   - Check if we can **distribute** at least `k` children with `mid` candies each ✅.  
   - If possible → **Increase `mid`** (move right ➡️)  
   - If not possible → **Decrease `mid`** (move left ⬅️)  

3️⃣ **Return the maximum valid `mid`** 🎉  

---

## **💻 Code Walkthrough**
```java []
public int maximumCandies(int[] candies, long k) {
    int start = 1, end = Integer.MIN_VALUE;

    // 🎯 Step 1: Find the maximum number of candies in a pile
    for (int val : candies) {
        end = Math.max(val, end);
    }

    int ans = 0;
    while (start <= end) {
        int mid = start + (end - start) / 2;  // 🔍 Mid-point (candidate answer)

        // 🛠️ Check if we can distribute 'mid' candies per child to at least 'k' children
        if (check(candies, k, mid)) {
            ans = mid;  // ✅ Valid answer found
            start = mid + 1;  // 🔼 Try for a larger answer
        } else {
            end = mid - 1;  // 🔽 Reduce search space
        }
    }
    return ans;  // 🎯 Maximum candies per child
}

// 🛠️ Helper function to check if 'mid' candies per child is possible
private boolean check(int[] candies, long k, int mid) {
    long count = 0;
    
    // 🍬 Count how many children can be satisfied with 'mid' candies per child
    for (int val : candies) {
        count += val / mid;  // Each pile contributes 'val / mid' children
        if (count >= k) return true;  // ✅ At least 'k' children satisfied
    }
    return false;
}
```

---

## **📌 Example Walkthrough**
### **Example 1**
**Input**:  
```java
candies = [5, 8, 6]
k = 3
```
**Process**:
- `max(candies[]) = 8`, so `start = 1`, `end = 8`  
- **Binary Search Iterations**:

| 🔢 `mid` | 🍬 Candies Distributed | ✅ Possible? | 🔄 Action |
|----------|----------------------|------------|----------|
| 4 | `5/4 + 8/4 + 6/4 = 1+2+1 = 4` ✅ | Move `start = mid + 1`  | (search right) |
| 6 | `5/6 + 8/6 + 6/6 = 0+1+1 = 2` ❌ | Move `end = mid - 1`| (search left) |
| 5 | `5/5 + 8/5 + 6/5 = 1+1+1 = 3` ✅ | Move `start = mid + 1` |(search right) |

**🎯 Final Answer**: `5`

---

## **⏳ Time and Space Complexity**
### **⌛ Time Complexity**:
1️⃣ **Binary Search Complexity**:  
   - Searching in `[1, max(candies[])]` takes **O(log max(candies[]))** ⏳.  

2️⃣ **Checking Function Complexity** (`check()` runs in O(n)):  
   - Iterating through `candies[]` in each step takes **O(n)**.

3️⃣ **Total Complexity**:  
   - **O(n log max(candies[]))** 🚀 (Efficient for large inputs).

### **📦 Space Complexity**:
- **O(1)** (Only a few integer variables used) ✅.

---

## **🌟 Key Takeaways**
✔ **Binary Search makes the problem efficient** 🏎️.  
✔ **Handles large values of `k` using `long` to prevent overflow** 📏.  
✔ **Optimal solution with `O(n log max(candies[]))` complexity** 🔥.  
['Binary Search', 'Java']