🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏| JAVA | C | C++ | PYTHON| JAVASCRIPT | DART
IntuitionThe problem requires us to distribute candies among k children such that each child gets the same number of candies, and we want to maximize this numbe
![image.png](https://assets.leetcode.com/users/images/4b2aa896-28ed-4ea4-9ef0-5ba4e556a204_1741924973.6815734.png)



---

# Intuition  
The problem requires us to distribute candies among `k` children such that each child gets the same number of candies, and we want to maximize this number.  

To solve this efficiently, we can use **binary search** on the number of candies each child receives. The minimum possible candies per child is `1`, and the maximum is determined by the **largest pile**.  

---

# Approach  
1. **Binary Search Setup**:  
   - The **search range** is `[1, max(candies)]`.  
   - If the total number of candies is **less than `k`**, we immediately return `0`, as it's impossible to distribute at least one candy per child.  

2. **Binary Search Execution**:  
   - We pick a middle value `mid` and check whether it's possible to distribute **at least `k`** candies per child (`canDistribute` function).  
   - If possible, move the **left boundary (`left = mid + 1`)** to search for a higher possible value.  
   - Otherwise, move the **right boundary (`right = mid - 1`)**.  

3. **Checking Distribution** (`canDistribute` function):  
   - We iterate over all candy piles and count how many children can be served if each child gets `mid` candies.  
   - If we can serve `k` children or more, return `true`, otherwise return `false`.  

---

# Complexity  
- **Time Complexity**:  
  - The **binary search** runs in **O(log M)**, where `M` is the max pile size.  
  - Checking distribution takes **O(N)**, where `N` is the number of candy piles.  
  - **Total Complexity**: **O(N log M)**.  

- **Space Complexity**: **O(1)** (No extra space used apart from variables).  

---


```dart []
import 'dart:math';

class Solution {
  int maximumCandies(List<int> candies, int k) {
    int left = 1, right = candies.reduce(max); // Maximum pile size
    if (candies.reduce((a, b) => a + b) < k) return 0; // Not enough candies

    int result = 0;

    while (left <= right) {
      int mid = (left + right) ~/ 2;
      if (canDistribute(candies, k, mid)) {
        result = mid;
        left = mid + 1; // Try a larger value
      } else {
        right = mid - 1; // Try a smaller value
      }
    }

    return result;
  }

  bool canDistribute(List<int> candies, int k, int val) {
    int count = 0;
    for (int c in candies) {
      count += c ~/ val;
    }
    return count >= k;
  }
}
```


```java []
class Solution {
    public int maximumCandies(int[] candies, long k) {
        int left = 1, right = 0;
        for (int c : candies) right = Math.max(right, c); // Find max candy pile
        long total = 0;
        for (int c : candies) total += c;
        if (total < k) return 0; // Not enough candies

        int result = 0;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (canDistribute(candies, k, mid)) {
                result = mid;
                left = mid + 1; // Try larger value
            } else {
                right = mid - 1; // Reduce value
            }
        }

        return result;
    }

    private boolean canDistribute(int[] candies, long k, int val) {
        long count = 0; 
        for (int c : candies) {
            count += c / val;
            if (count >= k) return true;  
        }
        return false;
    }
}

```


```cpp []
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    int maximumCandies(vector<int>& candies, int k) {
        int left = 1, right = *max_element(candies.begin(), candies.end());
        long long total = 0;
        for (int c : candies) total += c;
        if (total < k) return 0; // Not enough candies

        int result = 0;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (canDistribute(candies, k, mid)) {
                result = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return result;
    }

private:
    bool canDistribute(vector<int>& candies, int k, int val) {
        int count = 0;
        for (int c : candies) count += c / val;
        return count >= k;
    }
};
```


```python []
from typing import List

class Solution:
    def maximumCandies(self, candies: List[int], k: int) -> int:
        if sum(candies) < k: return 0  # Not enough candies
        
        left, right = 1, max(candies)
        result = 0
        
        while left <= right:
            mid = (left + right) // 2
            if self.canDistribute(candies, k, mid):
                result = mid
                left = mid + 1  # Try larger `mid`
            else:
                right = mid - 1  # Try smaller `mid`
        
        return result

    def canDistribute(self, candies: List[int], k: int, val: int) -> bool:
        return sum(c // val for c in candies) >= k
```


```javascript []
var maximumCandies = function(candies, k) {
    let left = 1, right = Math.max(...candies);
    let total = candies.reduce((sum, c) => sum + c, 0);
    if (total < k) return 0; // Not enough candies

    let result = 0;
    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        if (canDistribute(candies, k, mid)) {
            result = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    return result;
};

function canDistribute(candies, k, val) {
    let count = 0;
    for (let c of candies) count += Math.floor(c / val);
    return count >= k;
}
```

---




------

![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style='width:250px'}
['Array', 'Binary Search', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'Dart', 'Python ML']