✔️ Binary Search | Python | C++ | Java | JS | Go | C# | Swift
IntuitionThis problem asks us to find the maximum number of candies we can give to each of k children. Since we can divide piles but can't merge them, and we're
# Intuition
This problem asks us to find the **maximum number of candies** we can give to each of k children. Since we can divide piles but can't merge them, and we're looking for a maximum value, this is a perfect candidate for **binary search**.

# Approach
## Binary Search Setup:
  - `left = 1`: Minimum possible candies per child (if possible)
  - `right = max(candies)`: Maximum possible candies per child (limited by the largest pile)
  - `result = 0`: Variable to store our current best answer
## Binary Search Process:
  - For each middle value `mid`, calculate how many children can receive `mid` candies
  - This is done by summing up `pile / mid` for each pile
  - If we can satisfy at least `k` children, update our `result` to `mid` and search for larger values
  - Otherwise, search for smaller values

# Complexity
- Time complexity: `O(n * log(m))`
    - `n` is the length of the candies array
    - `m` is the maximum value in the candies array

- Space complexity: `O(1)`

# Code
```python3 []
class Solution:
    def maximumCandies(self, candies: List[int], k: int) -> int:        
        left, right = 1, max(candies)
        result = 0
        
        while left <= right:
            mid = (left + right) // 2
            
            children_count = sum(pile // mid for pile in candies)
            
            if children_count >= k:
                result = mid
                left = mid + 1
            else:
                right = mid - 1
                
        return result
```
```cpp []
class Solution {
public:
    int maximumCandies(vector<int>& candies, long long k) {
        long long left = 1, right = *max_element(candies.begin(), candies.end());
        int result = 0;

        while (left <= right) {
            long long mid = left + (right - left) / 2;
            long long children_count = 0;

            for (int pile : candies) {
                children_count += pile / mid;
            }

            if (children_count >= k) {
                result = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return result;
    }
};
```
```java []
class Solution {
    public int maximumCandies(int[] candies, long k) {
        int left = 1, right = 10_000_000;
        int result = 0;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            long childrenCount = 0;

            for (int candy : candies) {
                childrenCount += candy / mid;
            }

            if (childrenCount >= k) {
                result = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return result;
    }
}
```
```javascript []
var maximumCandies = function(candies, k) {
    let left = 1, right = Math.max(...candies);
    let result = 0;

    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        let childrenCount = candies.reduce((sum, pile) => sum + Math.floor(pile / mid), 0);

        if (childrenCount >= k) {
            result = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    return result;
};
```
```go []
func maximumCandies(candies []int, k int64) int {
    left, right := 1, 10_000_000
    result := 0

    for left <= right {
        mid := left + (right-left)/2
        var childrenCount int64 = 0

        for _, candy := range candies {
            childrenCount += int64(candy / mid)
        }

        if childrenCount >= k {
            result = mid
            left = mid + 1
        } else {
            right = mid - 1
        }
    }

    return result
}
```
```csharp []
public class Solution {
    public int MaximumCandies(int[] candies, long k) {
        int left = 1, right = candies.Max();
        int result = 0;

        while (left <= right) {
            int mid = (left + right) / 2;
            long childrenCount = 0;

            foreach (int candy in candies) {
                childrenCount += candy / mid;
            }

            if (childrenCount >= k) {
                result = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return result;
    }
}
```
```swift []
class Solution {
    func maximumCandies(_ candies: [Int], _ k: Int) -> Int {
        var left = 1
        var right = candies.max() ?? 0
        var result = 0

        while left <= right {
            let mid = (left + right) / 2
            let childrenCount = candies.reduce(0) { $0 + $1 / mid }

            if childrenCount >= k {
                result = mid
                left = mid + 1
            } else {
                right = mid - 1
            }
        }

        return result
    }
}
```
---
![upvote.png](https://assets.leetcode.com/users/images/2b34ea76-be6e-439b-a48a-471dac251c9b_1741913090.2472038.png)

['Binary Search', 'Swift', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#']